Huichao Liu
Huichao Liu

Reputation: 11

QT when I try to link to a dll, undefined reference error occurs

I build a cm-lib.dll

QT       -= gui
TARGET = cm-lib
TEMPLATE = lib
CONFIG += c++14
DEFINES += CMLIB_LIBRARY
......

.h file

#ifndef MASTERCONTROLLER_H
#define MASTERCONTROLLER_H

#include <QObject>
#include <QString>

#include "cm-lib_global.h"

namespace cm{
namespace controllers{

class MasterController : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString ui_welcomeMessage  MEMBER welcomeMessage CONSTANT)
public:
    explicit MasterController(QObject *parent = nullptr);
    QString welcomeMessage = "This is MasterController to Major Tom";

signals:

public slots:
};

}}

#endif // MASTERCONTROLLER_H

.cpp file

#include "master-controller.h"

namespace cm{
namespace controllers{


MasterController::MasterController(QObject *parent) : QObject(parent)
{
    int i = 1;
    i = i +1;
}

}}

When I try to link it in another main file, ld error

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlContext>

#include <controllers/master-controller.h>

int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    qmlRegisterType<cm::controllers::MasterController>("CM",1,0,"MasterController");

    cm::controllers::MasterController masterController(NULL);

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("masterController",&masterController);
    engine.load(QUrl(QStringLiteral("qrc:/views/MasterView.qml")));

    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

error message

D:\WORKC\QT\cm\cm-ui\source\main.cpp:18: error: undefined reference to cm::controllers::MasterController::MasterController(QObject*)' D:\WORKC\QT\shadow-builds\cm-ui\debug\main.o:-1: In function Z15qmlRegisterTypeIN2cm11controllers16MasterControllerEEiPKciiS4_': C:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtQml\qqml.h:285: error: undefined reference to cm::controllers::MasterController::staticMetaObject' C:\Qt\Qt5.10.0\5.10.0\mingw53_32\include\QtQml\qqml.h:308: error: undefined reference to cm::controllers::MasterController::staticMetaObject'

......

Upvotes: 0

Views: 535

Answers (1)

Huichao Liu
Huichao Liu

Reputation: 11

class CMLIBSHARED_EXPORT MasterController : public QObject

when I add CMLIBSHARED_EXPORT to the class definition,Problem solved!

Upvotes: 1

Related Questions