开发者

Using Dlls in Qt C++

I'm trying to build a project with a library (dll) I made. I've never attempted to either load or make a library before and I'm getting the following error.

error: undefined reference to `imp__ZN6NeuronC1Ev'

In Qt, the error is shown in the following line.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) <--------------------------- Error indicated here.
{
    ui->setupUi(this);
}

Project File

QT       += core gui

TARGET = Jane
TEMPLATE = app
LIBS += -L quote(C:\Programming\Jane\Jane\Source\Neuron.dll)

SOURCES += main.cpp\
        MainWindow.cpp

HEADERS  += MainWindow.h

FORMS    += MainWindow.ui

Here is one of the classes that I've exported

#ifndef NEURON_H
#define NEURON_H

#include <QList>
#include "Neuron_global.h"
#include <Sensor.h>

class NEURONSHARED_EXPORT Neuron
{
public:
    explicit Neuron();

    const double getOutput() const;
    const double & getWeight() const;

    void setWeight(const double& weight);
private:
    double weight;              // The weight of this neuron.

    QList<Neuron*> neurons;     // This Neuron's children.
    QList<Sensor*> sensors;     // This Neuron's Sensors.
};

#endif // NEURON_H

The NEURONSHARED_EXPORT Marco is defined in "Neuron_global.h"

#ifndef NEURON_GLOBAL_H
#define NEURON_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(NEURON_LIBRARY)
#  define NEURONSHARED_EXPORT Q_DECL_EXPORT
#else
#  define NEURONSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // NEURON_GLOBAL_H

If anyone has any advice on how to fix this, I would greatly appreciate it.

Edit: I've added the libNeuron.a file to the LIBS argumen开发者_StackOverflowt in pro file. However, I'm now getting the following error.

LIBS += libNeuron.a

cannot find -lNeuron.a

any ideas?


What are you trying to do?

LIBS += -L quote(C:\Programming\Jane\Jane\Source\Neuron.dll)

It variable contains a lib-files, which project will be linked with, not a library itself!

You should find a lib for the dll or use WINAPI LoadLibrary/GetProcAddres functions to load dll dynamically.


This is only a quick guess of mine: your trouble is caused c++ name mangling. Google "qt dll c++ name mangling" and find some examples of working dll / client projects.


In this case everything looks right (assuming NEURON_LIBRARY is not defined since you're building under the app template, although Windows v. Linux act differently in this regard).

qmake is known not to pick up all the changes that it ought to so I'd recommend re-running qmake and then your make variant (e.g. make, gmake, nmake):

$ qmake
$ nmake

In some cases, you'll actually need to do a clean (or delete the relevant object files) before everything will be able to link correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜