Qt:windows函数是未解析的外部符号

Net*_*ire 4 c++ windows winapi qt qt-creator

我正在尝试使用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;
}
Run Code Online (Sandbox Code Playgroud)

看起来很简单,但是当我尝试构建它时,编译失败了:

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
Run Code Online (Sandbox Code Playgroud)

我开始寻找,我找到了这个,但它根本没有帮助我,因为当我写下这个:

LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib"
Run Code Online (Sandbox Code Playgroud)

甚至这个:

LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib\\shell32.lib"
Run Code Online (Sandbox Code Playgroud)

在我看来.pro,这些"符号"仍然没有得到解决.我跑后qmake的每一个变化.pro-file内容.那么,有什么想法吗?

Fra*_*eld 9

-L设置DLL的搜索路径,但它实际上并不链接任何东西.实际链接是通过-l.不需要设置系统库的搜索路径,但是您需要链接到user32:

win32:LIBS += -luser32
Run Code Online (Sandbox Code Playgroud)


Fou*_*Two 5

除了弗兰克的回答(这对我很有帮助,谢谢!)我想补充一点,这只是 MSVC 所必需的,MinGW 似乎不需要该行。这对我来说是最令人困惑的部分,我首先认为我的 msvc 工具链有问题。

我的包含现在看起来像这样反映了这个事实:

msvc: LIBS += -luser32
Run Code Online (Sandbox Code Playgroud)