Native Windows API link problem on Qt + win32 + mingw

余生长醉 提交于 2020-01-13 05:49:27

问题


I am trying to use native windows API with Qt using mingw toolset. There are link problems with some functions. What happens? Is this a bug with mingw name mangling?

#ifdef Q_WS_WIN
    HWND hwnd = QWidget::winId();
    HDC hdcEMF  = CreateEnhMetaFile(NULL, NULL, NULL, NULL ) ;
    Rectangle(hdcEMF,100,100,200,200);
    HENHMETAFILE hemf = CloseEnhMetaFile(hdcEMF);
    OpenClipboard(hwnd);
    EmptyClipboard();
    SetClipboardData(CF_ENHMETAFILE,hemf);
    CloseClipboard();
#else   

The errors:

undefined reference to `CreateEnhMetaFileW@16'

undefined reference to `Rectangle@20'

undefined reference to `CloseEnhMetaFile@4'


回答1:


The functions CreateEnhMetaFileW() and CloseEnhMetaFile() are defined in the static library Gdi32.lib, so you have to make sure to link against that. Try adding -lgdi32 to the end of your command line you're using to compile. If that doesn't work, you might have to specify the full path to Gdi32.lib by adding -L/path/to/folder/containing/the/library -lgdi32 instead.




回答2:


If you want to use Windows API in a Qt app then there's no need to declare WinAPI functions extern "C", just include:

#include <qt_windows.h>

In your project file (.pro) add the libraries you use:

LIBS += -luser32 -lshell32 -lgdi32



回答3:


It's possible that the functions are included, but getting mangled due to the C++ assumption.

Look into the extern C { } declaration. It's intended to declare functions that should not be name mangled to account for polymorphism / overloading. (IE two functions with the same name).




回答4:


@torn your solution worked for me.

I wanted to use a win32 api call in my qt application.

the #include the #include your-win32-api-header

and finally the LIBS += -llibrary name.

Note that you might have to give -L for the right paths too.



来源:https://stackoverflow.com/questions/267672/native-windows-api-link-problem-on-qt-win32-mingw

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!