与MinGW(v.4.3.0)和libhid关联的问题 [英] Problems linking with MinGW (v.4.3.0) and libhid

查看:124
本文介绍了与MinGW(v.4.3.0)和libhid关联的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我正在Windows上使用Qt 5.9.3,为MinGW 32位构建. Qt部分是一个附带问题-问题似乎与MinGW有关. MinGW的版本是4.3.0,作为Qt安装的一部分预先提供.

Context: I'm using Qt 5.9.3 on Windows, building for MinGW 32-bit. The Qt part is a side issue though - the problem seems to be with MinGW. The version of MinGW is 4.3.0, supplied prebuilt as part of the Qt installation.

我正在建立一个通过HID与USB设备通信的库.一切都可以正常编译,但是在使用

I'm building a library which talks to a USB device over HID. Everything compiles fine, but it fails at the link stage with

./..\..\object\debug\usb_hid_device.o: In function `ZN8MyApp3USB5Win3213getDevicePathB5cxx11Ell':
<MYPATH>/../../source/win32/usb_hid_device.cpp:99: undefined reference to `HidD_GetAttributes(void*, _HIDD_ATTRIBUTES*)@8'
./..\..\object\debug\usb_hid_device.o: In function `ZN8MyApp3USB5Win3214CHIDDeviceImplC2EllRNS_15LogPerComponentE':
<MYPATH>/../../source/win32/usb_hid_device.cpp:200: undefined reference to `HidD_FlushQueue(void*)@4'

链接器命令为

g++ -shared -mthreads -Wl,-subsystem,windows -Wl,--out-implib,<MYPATH>\bin\debug\libusb_hid_comms.a -o <MYPATH>\bin\debug\usb_hid_comms.dll object_script.usb_hid_comms.Debug -lhid -lsetupapi -LC:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib C:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\libQt5Guid.a C:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\libQt5Cored.a

如果省略-lhid,则会出现相同的错误.如果删除-lhid并将路径和文件名显式设置为libhid.a,我也会遇到相同的错误.如果我故意输入错误的路径和文件名,则会出现错误,因此我知道命令行已正确解析.但是无论出于何种原因,MinGW似乎都没有与其自己的库文件之一进行链接.

If I omit -lhid I get the same errors. I also get the same errors if I remove -lhid and explicitly set the path and filename to libhid.a. If I deliberately mistype the path and filename, it comes up with an error, so I know the command-line is getting parsed correctly. But for whatever reason, MinGW appears to not be linking with one of its own library files.

我还尝试了删除-lsetupapi,并且得到了在那里定义的函数所期望的链接器错误.同样,Qt库文件.但是似乎专门针对libhid.a,MinGW可以看到该库文件,但不会链接它.

I've also tried removing -lsetupapi and I get the linker errors I'd expect for the functions defined in there. Likewise the Qt library files. But it seems that specifically for libhid.a, MinGW can see the library file but just isn't going to link with it.

还有其他人看到过吗?还是其他具有相同(或相似)版本的MinGW的人可以确认或拒绝他们可以与libhid.a链接吗?还是我的工作明显有问题?

Has anyone else seen this? Or can anyone else with the same (or similar) version of MinGW confirm or deny that they can link with libhid.a? Or is there something obviously wrong with what I'm doing?

推荐答案

我刚刚找到了答案.我自己发布了一个答案,以便将来其他人知道,因为我认为这仍然是一个人们可能想知道的有效问题.

I've just found the answer. I'm posting an answer myself so that other people know in future, because I think this is still a valid question which people might want to know about.

问题是包含文件hidsdi.h.引入Win32 API调用的大多数其他头文件在函数声明的周围都有extern "C".但是,这不是!结果是我们最终对链接器符号进行了C ++名称修饰,而不是链接器符号前的C样式"_".

The problem is the include file hidsdi.h. The majority of other header files which pull in Win32 API calls have extern "C" around the function declarations. However this one doesn't! The result is that we end up with C++ name mangling for linker symbols, instead of the C-style "_" in front of the linker symbols.

解决方案是使用

extern "C"
{
    #include <hidsdi.h>
}

然后一切正常.

具有较旧版本的MinGW(我正在从中移植)的hidsdi.h版本 did 具有对函数声明的保护.但是,看起来它已经在较新的版本中消失了.

The version of hidsdi.h with the older version of MinGW (which I'm porting from) did have that protection around the function declarations. However it looks like it's gone in the newer version.

这篇关于与MinGW(v.4.3.0)和libhid关联的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆