【问题标题】:Qt: windows functions are unresolved external symbolsQt:windows函数是未解析的外部符号
【发布时间】:2013-11-03 08:14:11
【问题描述】:

我正在尝试使用 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

我开始寻找,我找到了this,但它对我一点帮助都没有,因为当我写下这个时:

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。那么,有什么想法吗?

【问题讨论】:

  • 您肯定会错过与 Windows 库的链接。也许user32.lib

标签: c++ windows qt winapi qt-creator


【解决方案1】:

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

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

msvc: LIBS += -luser32

【讨论】:

    【解决方案2】:

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

    win32:LIBS += -luser32
    

    【讨论】:

    • 它使该行的其余部分特定于 Windows(以防您想在其他地方构建您的项目)
    猜你喜欢
    • 2014-04-20
    • 2013-03-04
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2015-11-17
    • 2011-02-09
    相关资源
    最近更新 更多