1

I have some functions in delphi dll, and I want to load them (using QtLibrary) at once.

Can I store that functions in global variables to use it? I tried to declare global function pointer in .h file and resolve them in main file, but got error "multiple definition"

mainwindow.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() {

}

mainwindow.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

error message:

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
3
  • If that's really what you have in the header file, then you should not get any multiple definition errors. The code you posted is apparently either inaccurate or incomplete. Post real code. Commented Jul 8, 2013 at 6:35
  • It may be an issue of guarding of header file.#ifndef #define #endif Commented Jul 8, 2013 at 6:40
  • The code looks good, but since it's not complete, we cannot be sure. The problem does not seem to be in the lines you posted here. Care to provide an SSCCE - including the full error message? Commented Jul 8, 2013 at 6:42

2 Answers 2

3

This error:

undefined reference to `nearestWell'

Is the compiler saying "I know that there is a thing called nearestWell, but I don't know where it is stored (because it hasn't been defined)"

This line:

extern NearestWellFunc nearestWell;

Says to the compiler "somewhere else there will be a NearestWellFunc called nearestWell".

This is a declaration - it is telling the compiler that there will be a variable called nearestWell somewhere later.

It needs to be paired with a definition that tells the compiler to set aside some space for the variable. In this case, it would look something like:

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

Remember, you can only define things once, or the compiler will get confused. This is why you need to put the declaration inside a .cpp file rather than the .h file - if you put the definition in the header file, each .cpp file that includes that header will include the definition, which is what is causing the multiple definition error.

Looking at your example structure, I would put the definition in mainwindow.cpp.

Sign up to request clarification or add additional context in comments.

Comments

2

You can declare the variables in a header file, but you can't define them.

For example, declaring a variable as extern in a header file is fine. You probably define them instead. Add the extern keyword to your declarations in the header file, and add definitions in a source file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.