[英]Qt: windows functions are unresolved external symbols
我正在嘗試使用QtCreator中的te WinAPI編譯一個簡單的類似於helloworld的非Qt C ++應用程序。 這是代碼:
#include <windows.h>
int main()
{
HWND cons = GetConsoleWindow();
SetWindowText(cons, L"I am the console window");
MessageBox(cons, L"Hello world!", L"I am the MessageBox", MB_OK | MB_ICONERROR);
return 0;
}
看起來很簡單,但是當我嘗試構建它時,編譯失敗並顯示:
main.obj:-1: error: LNK2019: unresolved external symbol __imp__MessageBoxW@16 referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol __imp__SetWindowTextW@8 referenced in function _main
我開始尋找,發現了這一點 ,但是它根本沒有幫助,因為當我寫這篇文章時:
LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib"
甚至這樣:
LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib\\shell32.lib"
在我的.pro
,這些“符號”仍然無法解析。 每次更改 .pro
-file內容后,我都運行qmake。 那么,有什么想法嗎?
-L
設置DLL的搜索路徑,但實際上不鏈接任何內容。 實際的鏈接是通過-l
完成的。 不必為系統庫設置搜索路徑,但是您需要針對user32進行鏈接:
win32:LIBS += -luser32
除了Frank的回答(這對我很有幫助,謝謝!)之外,我想補充一點,這僅是MSVC所必需的,MinGW似乎不需要該行。 對我來說,這是最令人困惑的部分,我首先認為我在使用msvc工具鏈時遇到了問題。
現在,我的加入看起來像這樣以反映這一事實:
msvc: LIBS += -luser32
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.