簡體   English   中英

Qt無法與g ++鏈接

[英]Qt cannot linking with g++

大家好,這是我第一次在這里提問

我試圖用g ++編譯簡單的Qt c ++代碼,但是每次它給我:

C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text+0x32): undefined reference to `_imp___ZN12QApplicationC1ERiPPci'
C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text+0x6e): undefined reference to `_imp___ZN11QPushButtonC1ERK7QStringP7QWidget'
C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text+0xa6): undefined reference to `_imp___ZN7QWidget4showEv'
C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text+0xad): undefined reference to `_imp___ZN12QApplication4execEv'
C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text+0xbb): undefined reference to `_imp___ZN12QApplicationD1Ev'
C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text+0xe9): undefined reference to `_imp___ZN12QApplicationD1Ev'
C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text$_ZN7QStringC1EPKc[__ZN7QStringC1EPKc]+0x2c): undefined reference to `_imp___ZN7QString16fromAscii_helperEPKci'
C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text$_ZN7QWidget6resizeEii[__ZN7QWidget6resizeEii]+0x2f): undefined reference to `_imp___ZN7QWidget6resizeERK5QSize'
C:\Users\ctw\AppData\Local\Temp\ccrszXqF.o:main.cpp:(.text$_ZN15QTypedArrayDataItE10deallocateEP10QArrayData[__ZN15QTypedArrayDataItE10deallocateEP10QArrayData]+0x1d): undefined reference to `_imp___ZN10QArrayData10deallocateEPS_jj'

編譯命令為:

g++ -o main.exe  main.cpp C:\Qt_\5.0.2\msvc2010\lib\Qt5Core.lib C:\Qt_\5.0.2\msvc2010\lib\Qt5Core.lib

我嘗試編譯的代碼是:

#include <QtWidgets/qapplication.h>
#include <QtWidgets/qpushbutton>
int main(int argc, char *argv[])
{
     QApplication app(argc, argv);
     QPushButton *hello = new QPushButton("This is a simple button");
     hello->resize(100, 30);
     hello->show();
     return app.exec();
}

g ++的版本為:

g++ (GCC) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我正在使用Qt 5.0.2 32bit。 我是C ++的初學者,因此嘗試解決了大約一周的時間。 誰能告訴我應該怎么做才能成功編譯?

除非您有理由不升級您的Qt安裝,否則請這樣做,因為5.0.2確實是舊的並且有錯誤,我們現在的版本是5.8。 現在,解決您的問題:

您正在嘗試使用不同的編譯器來編譯應用程序,請參見Qt是使用Visual Studio進行編譯的(您可以通過嘗試手動鏈接的庫的路徑來告知這一點)。

另一件事,Qt的文件通常傳遞給預處理狀態發送到編譯器之前,我知道你的特定代碼沒有這個錯誤還沒有 ,但只要你增加代碼大小,你會打的,所以使用了解Qt內部知識的項目經理,例如qmake,qbs或CMake(我更喜歡后者),這將選擇正確的Qt套件並在編譯應用程序之前運行moc。

最后,如果使用Visual Studio版本的Qt,則需要使用cl編譯器而不是Gcc進行編譯。 如果您下載Qt Installer,它將詢問您是否具有mingw版本(g ++隨附)或Visual Studio版本。

對於您的問題,您需要使用Qt庫的“ MinGW”版本。 實際上,您只能使用Visual Studio 2010編譯器來編譯項目。

您可以在此處找到正確的庫: Qt 5.0.2存檔

另外,您將需要MinGW 4.7。

暫無
暫無

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

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