Qt中的第一个简单应用程序可以编译但无法链接

时间:2019-01-19 20:31:06

标签: c++ qt qt-creator

我想尝试一下Qt。通常使用VC ++ 2017,因此我安装了Qt版本5.12,并使用创建器创建了一个新的Qt cmd line应用程序。但是,当创建派生自QObject的最简单的类时,我得到许多与某些metaObject类有关的链接器错误。这没有什么意义,与VisualStudio相比,整个编译/链接/调试过程似乎非常复杂。我知道这是非常不同的,但是请有人可以帮忙吗?我收到以下错误:

02:53:46: Running steps for project FirstConsoleApp...
02:53:46: Starting: "C:\Development\Qt\Qt5.12.0\5.12.0\msvc2017_64\bin\qmake.exe" D:\Development\Qt\FirstConsoleApp\FirstConsoleApp.pro -spec win32-msvc "CONFIG+=qtquickcompiler"
Info: creating stash file D:\Development\Qt\build-FirstConsoleApp-Desktop_Qt_5_12_0_MSVC2017_64bit-Release.qmake.stash
02:53:47: The process "C:\Development\Qt\Qt5.12.0\5.12.0\msvc2017_64\bin\qmake.exe" exited normally.
02:53:47: Starting: "C:\Development\Qt\Qt5.12.0\Tools\QtCreator\bin\jom.exe" -f D:/Development/Qt/build-FirstConsoleApp-Desktop_Qt_5_12_0_MSVC2017_64bit-Release/Makefile qmake_all

jom 1.1.2 - empower your cores

02:53:47: The process "C:\Development\Qt\Qt5.12.0\Tools\QtCreator\bin\jom.exe" exited normally.
02:53:47: Starting: "C:\Development\Qt\Qt5.12.0\Tools\QtCreator\bin\jom.exe"
C:\Development\Qt\Qt5.12.0\Tools\QtCreator\bin\jom.exe -f Makefile.Release
cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -Zc:referenceBinding -Zc:__cplusplus -O2 -MD -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -DNDEBUG -I..\FirstConsoleApp -I. -IC:\Development\Qt\Qt5.12.0\5.12.0\msvc2017_64\include -IC:\Development\Qt\Qt5.12.0\5.12.0\msvc2017_64\include\QtCore -Irelease -IC:\Development\Qt\Qt5.12.0\5.12.0\msvc2017_64\mkspecs\win32-msvc -Forelease\ @C:\Users\PRZEMY~1\AppData\Local\Temp\main.obj.1784.15.jom
main.cpp
..\FirstConsoleApp\main.cpp(105): warning C4100: 'order': unreferenced formal parameter
..\FirstConsoleApp\main.cpp(30): warning C5046: 'anonymous-namespace'::QOrderCache::metaObject': Symbol involving type with internal linkage not defined ..\FirstConsoleApp\main.cpp(30): warning C5046: 'anonymous-namespace'::QOrderCache::qt_metacast': Symbol involving type with internal linkage not defined
..\FirstConsoleApp\main.cpp(30): warning C5046: 'anonymous-namespace'::QOrderCache::qt_metacall': Symbol involving type with internal linkage not defined ..\FirstConsoleApp\main.cpp(16): warning C5046: 'anonymous-namespace'::QOrder::metaObject': Symbol involving type with internal linkage not defined
..\FirstConsoleApp\main.cpp(16): warning C5046: 'anonymous-namespace'::QOrder::qt_metacast': Symbol involving type with internal linkage not defined ..\FirstConsoleApp\main.cpp(16): warning C5046: 'anonymous-namespace'::QOrder::qt_metacall': Symbol involving type with internal linkage not defined
..\FirstConsoleApp\main.cpp(90): warning C5046: 'anonymous-namespace'::QTradingService::metaObject': Symbol involving type with internal linkage not defined ..\FirstConsoleApp\main.cpp(90): warning C5046: 'anonymous-namespace'::QTradingService::qt_metacast': Symbol involving type with internal linkage not defined
..\FirstConsoleApp\main.cpp(90): warning C5046: 'anonymous-namespace'::QTradingService::qt_metacall': Symbol involving type with internal linkage not defined link /NOLOGO /DYNAMICBASE /NXCOMPAT /INCREMENTAL:NO /SUBSYSTEM:CONSOLE "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /MANIFEST:embed /OUT:release\FirstConsoleApp.exe @C:\Users\PRZEMY~1\AppData\Local\Temp\FirstConsoleApp.exe.1784.2484.jom main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdeclanonymous namespace'::QOrder::metaObject(void)const " (?metaObject@QOrder@?A0x87dcd2e0@@UEBAPEBUQMetaObject@@XZ)
main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl anonymous namespace'::QOrder::qt_metacast(char const *)" (?qt_metacast@QOrder@?A0x87dcd2e0@@UEAAPEAXPEBD@Z) main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdeclanonymous namespace'::QOrder::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@QOrder@?A0x87dcd2e0@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl anonymous namespace'::QOrderCache::metaObject(void)const " (?metaObject@QOrderCache@?A0x87dcd2e0@@UEBAPEBUQMetaObject@@XZ) main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdeclanonymous namespace'::QOrderCache::qt_metacast(char const *)" (?qt_metacast@QOrderCache@?A0x87dcd2e0@@UEAAPEAXPEBD@Z)
main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl anonymous namespace'::QOrderCache::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@QOrderCache@?A0x87dcd2e0@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdeclanonymous namespace'::QTradingService::metaObject(void)const " (?metaObject@QTradingService@?A0x87dcd2e0@@UEBAPEBUQMetaObject@@XZ)
main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl anonymous namespace'::QTradingService::qt_metacast(char const *)" (?qt_metacast@QTradingService@?A0x87dcd2e0@@UEAAPEAXPEBD@Z) main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdeclanonymous namespace'::QTradingService::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@QTradingService@?A0x87dcd2e0@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
release\FirstConsoleApp.exe : fatal error LNK1120: 9 unresolved externals
jom: D:\Development\Qt\build-FirstConsoleApp-Desktop_Qt_5_12_0_MSVC2017_64bit-Release\Makefile.Release [release\FirstConsoleApp.exe] Error 1120
jom: D:\Development\Qt\build-FirstConsoleApp-Desktop_Qt_5_12_0_MSVC2017_64bit-Release\Makefile [release] Error 2
02:53:50: The process "C:\Development\Qt\Qt5.12.0\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project FirstConsoleApp (kit: Desktop Qt 5.12.0 MSVC2017 64bit)
When executing step "Make"
02:53:50: Elapsed time: 00:04.

我认为我已经在“套件”部分中正确设置了变量。不太确定出什么问题了。似乎我缺少基本知识,忘记为链接器输入添加lib或某些东西,一点都不知道。 非常感谢您提供任何提示。

这实际上是一个简单的项目,只有一个main.cpp文件,并且只有一个类来自QObject。
这是我的专业文件的内容:

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    order.h

这是main.cpp文件的内容:

#include<iostream>
#include<functional>
#include<queue>

#include<QCoreApplication>
#include<QSharedPointer>
#include<QObject>

namespace {

typedef  std::map<unsigned, unsigned, std::function<bool(unsigned,unsigned)>> bid_ask_cache_type;
enum eBidAsk { eBid=-1, eAsk=1, eNone=0};

struct QOrder : public QObject
{
    Q_OBJECT

public:
    eBidAsk type = eBidAsk::eNone;
    unsigned quantity, price;

    QOrder(eBidAsk required_type, unsigned required_quantity, unsigned required_price)
        :type(required_type), quantity(required_quantity), price(required_price) {}

    virtual ~QOrder() {}
};

}

void testClasses()
{
    QOrder order(eBidAsk::eAsk, 100, 100);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    testClasses();
    return a.exec();
}

真的很特别。我只按Build All,我应该运行所有需要的重新编译步骤,不是吗?整个Qt与我过去使用的VC ++或Borland不同。非常感谢您的帮助

0 个答案:

没有答案