1

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

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
4

2 に答える 2

3

このエラー:

「nearestWell」への未定義の参照

コンパイラは「 というものがあることは知っているがnearestWell、どこに格納されているかは知らない (定義されていないため)」と言っているのですか?

この行:

extern NearestWellFunc nearestWell;

コンパイラーに「どこか別の場所でNearestWellFunc呼び出されるnearestWell」と言います。

これは宣言です。後でどこかで呼び出される変数があることをコンパイラに伝えています。nearestWell

これは、変数用にスペースを確保するようコンパイラーに指示する定義と組み合わせる必要があります。この場合、次のようになります。

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

一度しか定義できないことを覚えておいてください。そうしないと、コンパイラが混乱します。これが、宣言を .h ファイルではなく .cpp ファイル内に配置する必要がある理由です。定義をヘッダー ファイルに配置すると、そのヘッダーを含む各 .cpp ファイルに定義が含まれます。複数定義エラー。

あなたの構造例を見て、定義をmainwindow.cppに入れます。

于 2013-07-08T07:13:37.893 に答える
2

変数はヘッダー ファイルで宣言できますが、定義することはできません。

たとえばextern、ヘッダー ファイルのように変数を宣言しても問題ありません。おそらく代わりにそれらを定義します。ヘッダー ファイルの宣言にキーワードを追加externし、ソース ファイルに定義を追加します。

于 2013-07-08T06:22:06.170 に答える