【问题标题】:Global function pointer. multiple definition error全局函数指针。多重定义错误
【发布时间】:2013-07-08 06:19:31
【问题描述】:

我在 delphi dll 中有一些函数,我想一次加载它们(使用 QtLibrary)。

我可以将该函数存储在全局变量中以使用它吗?我试图在 .h 文件中声明全局函数指针并在主文件中解析它们,但得到错误“多重定义”

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qlibrary.h>
#include <QDebug>
#include "mapwidget.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    QString path = "map_dll.dll";
    if (QLibrary::isLibrary(path)) {
        lib = new QLibrary(path);
        lib->load();
        if (!lib->isLoaded()) {
            qDebug() << lib->errorString();
        } else {
           nearestWell = (void( *)(double x,
                                double y,
                                double &wellX,
                                double &wellY))
                lib->resolve("nearestWell");

    }
}

}

void MainWindow::on_pushButton_2_clicked() {

}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLibrary>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QLibrary *lib;

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mapwidget.h

#ifndef MAPWIDGET_H
#define MAPWIDGET_H

#include <QWidget>
#include <QPainter>


typedef void (*NearestWellFunc) (double x, double y, double &wellX,
                             double &wellY);
extern NearestWellFunc nearestWell;

....
#endif // MAPWIDGET_H

错误信息:

debug/mainwindow.o: In function `ZN10MainWindow21on_pushButton_clickedEv':
C:\nipi\APT\map_qt\build-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:33: undefined reference to `nearestWell'
C:\nipi\APT\map_qt\build-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:40: undefined reference to `nearestWell'
C:\nipi\APT\map_qt\build-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:54: undefined reference to `nearestWell'
Makefile.Debug:84: recipe for target 'debug/MIG.exe' failed
mingw32-make[1]: Leaving directory 'C:/nipi/APT/map_qt/build-    MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug'
makefile:34: recipe for target 'debug' failed
C:\nipi\APT\map_qt\build-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:63: undefined reference to `nearestWell'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [debug/MIG.exe] Error 1
mingw32-make: *** [debug] Error 2

【问题讨论】:

  • 如果这确实是您在头文件中的内容,那么您应该不会收到任何多重定义错误。您发布的代码显然不准确或不完整。贴出真实代码。
  • 可能是guarding of headerfile.#ifndef #define #endif的问题
  • 代码看起来不错,但由于它不完整,我们无法确定。问题似乎不在您在此处发布的行中。是否愿意提供SSCCE - 包括full 错误消息?

标签: c++ qt function-pointers


【解决方案1】:

这个错误:

对 `nearestWell' 的未定义引用

编译器是否在说“我知道有一个东西叫nearestWell,但我不知道它存储在哪里(因为它还没有被定义)”

这一行:

extern NearestWellFunc nearestWell;

对编译器说“在其他地方会有一个名为 NearestWellFuncnearestWell”。

这是一个声明 - 它告诉编译器将有一个名为nearestWell 的变量稍后。

它需要与一个定义配对,告诉编译器为变量留出一些空间。在这种情况下,它看起来像:

NearestWellFunc nearestWell = /* whatever the initial value should be */

记住,你只能定义一次,否则编译器会混淆。这就是为什么您需要将声明放在 .cpp 文件而不是 .h 文件中的原因 - 如果将定义放在头文件中,则包含该头文件的每个 .cpp 文件都将包含该定义,这就是导致多重定义错误。

查看您的示例结构,我会将定义放在 mainwindow.cpp 中。

【讨论】:

    【解决方案2】:

    您可以在头文件中声明变量,但不能定义它们。

    例如,在头文件中将变量声明为extern 就可以了。您可能改为定义它们。在头文件的声明中添加extern 关键字,并在源文件中添加定义。

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 2011-08-30
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多