全局函数指针.多重定义错误

Global function pointer. multiple definition error

本文关键字:定义 错误 函数 指针 全局      更新时间:2023-10-16

我在delphidll中有一些函数,我想一次加载它们(使用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主窗口::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:nipiAPTmap_qtbuild-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:33: undefined reference to `nearestWell'
C:nipiAPTmap_qtbuild-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:40: undefined reference to `nearestWell'
C:nipiAPTmap_qtbuild-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:nipiAPTmap_qtbuild-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

此错误:

对"nearestWell"的未定义引用

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

此行:

extern NearestWellFunc nearestWell;

对编译器说"在其他地方会有一个NearestWellFunc,叫做nearestWell"。

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

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

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

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

看看你的示例结构,我会把定义放在mainwindow.cpp.

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

例如,在头文件中将变量声明为extern是可以的。你可能会定义它们。将extern关键字添加到头文件中的声明中,并在源文件中添加定义。