我在 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");
}
}
}
无效 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
地图小部件.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