[英]Qt: windows functions are unresolved external symbols
I'm trying to compile a simple helloworld-like non-Qt C++ application using te WinAPI in QtCreator. 我正在尝试使用QtCreator中的te WinAPI编译一个简单的类似于helloworld的非Qt C ++应用程序。 Here's the code:
这是代码:
#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;
}
Looks very simple, but when I've tried to build it, the compilation fails with: 看起来很简单,但是当我尝试构建它时,编译失败并显示:
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
I started to seek, and I found this , but it wasn't helping me at all, because when I had written this: 我开始寻找,发现了这一点 ,但是它根本没有帮助,因为当我写这篇文章时:
LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib"
and even this: 甚至这样:
LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib\\shell32.lib"
in my .pro
, these "symbols" still stand unresolved. 在我的
.pro
,这些“符号”仍然无法解析。 I ran qmake after each change to the .pro
-file contents. 每次更改
.pro
-file内容后,我都运行qmake。 So, any ideas? 那么,有什么想法吗?
-L
sets the search paths for DLLs, but it doesn't actually link anything. -L
设置DLL的搜索路径,但实际上不链接任何内容。 The actual linking is done via -l
. 实际的链接是通过
-l
完成的。 Setting the search path for system libraries shouldn't be necessary, but you'll need to link against user32: 不必为系统库设置搜索路径,但是您需要针对user32进行链接:
win32:LIBS += -luser32
Additional to Frank's answer (which helped me very much, thanks for that!) I would like to add that this is only required vor MSVC, MinGW doesn't seem to need that line. 除了Frank的回答(这对我很有帮助,谢谢!)之外,我想补充一点,这仅是MSVC所必需的,MinGW似乎不需要该行。 Which was for me the most confusing part, I first thought I had problems with the msvc toolchain.
对我来说,这是最令人困惑的部分,我首先认为我在使用msvc工具链时遇到了问题。
My inclusion now looks like this to reflect this fact: 现在,我的加入看起来像这样以反映这一事实:
msvc: LIBS += -luser32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.