Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

c++ - 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. 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. I ran qmake after each change to the .pro-file contents. So, any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

-L sets the search paths for DLLs, but it doesn't actually link anything. The actual linking is done via -l. Setting the search path for system libraries shouldn't be necessary, but you'll need to link against user32:

win32:LIBS += -luser32

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...