mingw报未定义引用gdi32中函数,undefined reference to `__imp_CreateCompatibleDC‘错误解决办法

PDFium编译错误修复: Mingw缺失Win32库链接
在尝试编译PDFium项目时,使用Mingw遇到了多个链接错误,主要涉及到Win32 API函数如`CreateCompatibleDC`、`DeleteDC`等未定义。解决方法是在makefile中添加`-lgdi32`链接库,确保编译成功并能正确调用Windows图形设备接口。

编译pdfium-master时,mingw老是报下面这些错误

PS D:\projectcode\PDFium-master\biuld> make fx_win32_c_all
 compiling:    fx_win32_device.o  fx_win32_dib.o  fx_win32_dwrite.o  fx_win32_gdipext.o  fx_win32_print.o
PS D:\projectcode\PDFium-master\biuld> make
../sdk/src/fpdf_dataavail.cpp: In member function 'virtual FX_BOOL CFPDF_FileAccessWrap::ReadBlock(void*, FX_INT32, size_t)':
../sdk/src/fpdf_dataavail.cpp:64:55: warning: pointer of type 'void *' used in arithmetic [-Wpointer-arith]
                 memcpy(buffer, m_pFileAccess->m_Param + offset, size);
                                ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
../sdk/src/fpdf_dataavail.cpp: In member function 'virtual FX_BOOL CPDF_CustomAccess::ReadBlock(void*, FX_INT32, size_t)':
../sdk/src/fpdf_dataavail.cpp:108:95: warning: no return statement in function returning non-void [-Wreturn-type]
         virtual FX_BOOL ReadBlock(void *buffer, FX_FILESIZE offset, size_t size) FX_OVERRIDE {
   
   }
                                                                                               ^
fx_win32_device.o:fx_win32_device.cpp:(.text+0xf8): undefined reference to `__imp_CreateCompatibleDC'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x250): undefined reference to `__imp_DeleteDC'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x290): undefined reference to `__imp_CreateFontIndirectA'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x3aa): undefined reference to `__imp_CreateFontIndirectA'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x83f): undefined reference to `__imp_EnumFontFamiliesExA'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x1c33): undefined reference to `__imp_CreateFontA'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x1c5f): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x1c8c): undefined reference to `__imp_GetTextFaceA'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x1cb0): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x1e3a): undefined reference to `__imp_DeleteObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x202a): undefined reference to `__imp_CreateFontA'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x216c): undefined reference to `__imp_DeleteObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x21a4): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x2208): undefined reference to `__imp_GetFontData'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x2223): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x2269): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x228d): undefined reference to `__imp_GetTextFaceA'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x22a8): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x2300): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x231c): undefined reference to `__imp_GetTextMetricsA'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x2334): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x281c): undefined reference to `__imp_SetStretchBltMode'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x283a): undefined reference to `__imp_GetObjectType'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x2870): undefined reference to `__imp_CreateBitmap'
fx_win32_device.o:fx_win32_device.cpp:(.text+0x288f): undefined reference to `__imp_SelectObject'
fx_win32_device.o:fx
<think>我们正在处理一个Qt编译错误`undefined reference to '__imp_MSG'`。这个错误通常发生在链接阶段,意味着编译器知道某个函数的声明(通过头文件),但在链接时找不到其实现(通常是库文件未正确链接)。 根据错误信息中的`__imp_`前缀,这通常与Windows平台下的动态库导入有关。`MSG`可能是某个库中的函数或变量。 结合用户提供的引用内容(虽然不完全相同,但可以参考): 引用[1]和[2]提到了`undefined reference`错误,通常与库链接问题有关。 引用[3]则展示了一个典型的库链接问题,其中缺少了多线程库(pthread)和其他自定义函数。 因此,我们可以推断出:错误`undefined reference to '__imp_MSG'`很可能是因为缺少了某个库,该库中包含了`MSG`符号的定义。 在Windows编程中,`MSG`结构通常与Win32 API的消息处理相关(定义在`windows.h`中)。但是,这里我们遇到的是链接错误,而不是未定义类型。所以,更可能是与某个使用Win32消息的库有关。 然而,在Qt中,我们通常不会直接链接Win32的库(除非使用了特定于平台的代码)。因此,我们需要考虑两种情况: 1. 项目中使用了Win32 API,并且需要链接相应的库(如`user32.lib`等)。 2. 项目中使用了某个第三方库,该库又依赖Win32的库。 具体到错误`__imp_MSG`,这个符号可能是Win32`MSG`结构相关的函数或变量?但实际上,`MSG`是一个结构体类型,通常不会直接产生链接错误。所以,这个错误更可能是某个函数使用`MSG`结构,而该函数的实现在某个库中,但我们没有链接这个库。 在Win32 API中,与`MSG`相关的函数主要是`GetMessage`, `PeekMessage`, `TranslateMessage`, `DispatchMessage`等。这些函数位于`user32.dll`中,对应的导入库为`user32.lib`。 因此,解决方案可能是:在Qt项目中添加对`user32.lib`的链接。 在Qt项目文件(.pro)中,可以使用`LIBS`变量来添加库: ```qmake LIBS += -luser32 ``` 但是,请注意:如果使用的是MinGW编译器,那么库名可能是`libuser32.a`,所以也可以这样写: ```qmake LIBS += -L"path_to_libs" -luser32 ``` 不过,通常MinGW的库路径已经在系统路径中,所以只需要`-luser32`即可。 另外,如果项目中没有直接使用Win32 API,而是Qt内部使用了,那么通常Qt会自动链接必要的库。但有时候,如果我们在项目中直接包含了`windows.h`使用了Win32 API,就需要手动链接。 因此,步骤总结如下: 1. 检查代码中是否使用了Win32 API(例如`GetMessage`等)。如果使用了,确保包含`windows.h`头文件,并且在项目文件中链接`user32`库。 2. 如果没有直接使用,那么可能是某个第三方库依赖了`user32`,同样需要链接。 在Qt项目文件(.pro)中添加: ```qmake LIBS += -luser32 ``` 3. 重新构建项目。 如果问题依旧,那么可能需要检查库的路径是否包含在链接器的搜索路径中。 另外,根据引用[2]和[3]的启示,也可能是其他库的缺失,但由于错误信息明确指向`__imp_MSG`,所以首先考虑`user32`。 如果添加了`user32`仍然不行,那么考虑: - MSG是否可能是其他库中的符号?比如OpenSSL库中有个`MSG`结构(在消息摘要相关函数中)?但通常OpenSSL中的相关函数不会以`__imp_MSG`形式出现。 - 或者,是否项目中定义了一个名为`MSG`的变量或函数,但是没有实现(即没有定义)? 不过,根据错误信息中的`__imp_`,这明确表示是动态库导入符号,所以应该是缺少了某个系统库或第三方库。 因此,优先尝试链接`user32`。 如果用户使用的是Linux,则不会出现`__imp_`这样的前缀(因为这是Windows的导入库前缀),所以可以确定是Windows平台的问题。 最后,如果上述方法无效,请检查代码中是否有拼写错误,或者是否在64位/32位库上出现了混淆(比如链接了64位的库而项目是32位的,反之亦然)。 另外,参考引用[1]中提到的错误,他们通过添加库的链接解决了问题,所以我们也可以尝试类似的思路。 生成相关问题: 1. Qt中如何链接Windows系统库? 2. 如何解决Qt中的undefined reference错误? 3. Qt项目中的LIBS变量如何使用? 4. Windows平台下Qt开发需要链接哪些常用库? </think>### Qt中解决`undefined reference to '__imp_MSG'`错误的方法 #### 问题原因分析(Windows平台特有) 该错误表明链接器无法找到`MSG`符号的实现,通常发生在: 1. **Windows API库未链接**:`MSG`Windows消息处理的核心结构体(定义于`winuser.h`),需要链接`user32.lib`[^1] 2. **Qt项目配置缺失**:未在.pro文件中指定Windows API库 3. **编译器路径问题**:MinGW/MSVC的库路径配置错误 4. **32/64位库混用**:32位程序链接了64位库(或反之) --- #### 解决方案步骤 ##### 1. 添加Windows系统库链接(关键步骤) 在Qt项目文件(.pro)中添加: ```qmake # 链接Windows GUI子系统 LIBS += -luser32 LIBS += -lgdi32 # 可选:如果涉及图形操作 LIBS += -lkernel32 # 可选:如果需要内核API ``` ##### 2. 验证头文件包含 确保在使用Windows API的源文件中包含: ```cpp #include <windows.h> // 包含MSG结构体定义 #include <winsock2.h> // 如果涉及网络功能 ``` ##### 3. 清理并重建项目 ```bash qmake -makefile # 重新生成Makefile make clean # 清理旧构建 make # 重新编译 ``` ##### 4. 检查编译器配置 - **MinGW用户**:确认`libuser32.a`存在于MinGW的lib目录(如`C:\Qt\Tools\mingw530_32\i686-w64-mingw32\lib`) - **MSVC用户**:在.pro文件中添加: ```qmake QMAKE_LFLAGS += /SUBSYSTEM:WINDOWS LIBS += user32.lib ``` ##### 5. 验证平台一致性 确保: 1. Qt Creator的构建套件(Kit)与项目架构匹配 2. 所有第三方库均为相同架构(32/64位) --- #### 典型错误场景修复 ```cpp // 错误示例:未链接user32 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MSG msg; // 链接时错: undefined reference to '__imp_MSG' } // 解决方案:在.pro中添加 LIBS += -luser32 ``` #### 预防措施 1. **添加平台检测**: ```cpp #ifdef Q_OS_WIN #include <windows.h> #endif ``` 2. **使用Qt封装替代原生API**: ```cpp // 使用QMessage替代Windows MSG QApplication::postEvent(receiver, new QEvent(QEvent::User)); ``` 3. **静态链接Windows库**: ```qmake # 在.pro文件中 CONFIG += static ``` --- #### 扩展说明 `__imp_`前缀表示该符号是Windows DLL的导入函数[^2]。其他常见相关错误: - `__imp_WSAStartup` → 链接`ws2_32.lib` - `__imp_CreateWindowEx` → 链接`user32.lib` - `__imp_RegOpenKeyEx` → 链接`advapi32.lib` Qt的Windows开发推荐使用`QWinEventNotifier`替代原生API调用: ```cpp // 安全处理Windows消息 QWinEventNotifier *notifier = new QWinEventNotifier(eventHandle); connect(notifier, &QWinEventNotifier::activated, [](){ qDebug() << "Windows event received"; }); ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值