簡體   English   中英

QT 當我嘗試鏈接到 dll 時,出現未定義的引用錯誤

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

我建立了一個 cm-lib.dll

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

.h 文件

#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 文件

#include "master-controller.h"

namespace cm{
namespace controllers{


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

}}

當我嘗試將其鏈接到另一個主文件時,ld 錯誤

#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();
}

錯誤信息

D:\WORKC\QT\cm\cm-ui\source\main.cpp:18: 錯誤:未定義對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靜態元對象'

……

class CMLIBSHARED_EXPORT MasterController : public QObject

當我在 class 定義中添加CMLIBSHARED_EXPORT時,問題解決了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM