#include < WinUsb.h> 在 MFC 应用中产生编译器错误
#include <WinUsb.h> produces compiler error in MFC app
我有一个由VS2010生成的通用SDI应用程序,我想用它来测试WinUsb API。我已经安装了当前版本的WDK。根据我在其他地方读到的帖子,我应该能够设置VS2010项目包含路径指向WDK并添加包含WinUsb.h来做到这一点。所以我将它添加到stdafx.h中,如下所示
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <Winusb.h>
但是当我编译时,我得到错误
1>c:winddk7600.16385.1incapiusb200.h(93): error C2332: 'struct' : missing tag name
1>c:winddk7600.16385.1incapiusb200.h(93): error C2011: '<unnamed-tag>' : 'enum' type redefinition
1> c:program files (x86)microsoft sdkswindowsv7.0aincludehtmlhelp.h(331) : see declaration of '<unnamed-tag>'
1>c:winddk7600.16385.1incapiusb200.h(93): error C2059: syntax error : 'constant'
1>c:winddk7600.16385.1incapiusb200.h(93): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
它所抱怨的结构是
typedef union _USB_HIGH_SPEED_MAXPACKET {
struct _MP {
USHORT MaxPacket:11; /* 0..10 */
USHORT HSmux:2; /* 11..12 */
USHORT Reserved:3; /* 13..15 */
};
USHORT us;
} USB_HIGH_SPEED_MAXPACKET, *PUSB_HIGH_SPEED_MAXPACKET;
, IDE用红色下划线标注_MP
问题的根源在于当应用被配置为使用MBCS时。在选择MBCS时包含的mbctype.h
内部是一组#define
语句
/* bit masks for MBCS character types */
#define _MS 0x01 /* MBCS single-byte symbol */
#define _MP 0x02 /* MBCS punct */
#define _M1 0x04 /* MBCS 1st (lead) byte */
#define _M2 0x08 /* MBCS 2nd byte*/
#define _SBUP 0x10 /* SBCS upper char */
#define _SBLOW 0x20 /* SBCS lower char */
发生在usb200.h
之前。您可以不选择MBCS(通过使用Unicode),也可以按照Lynn所说的做,并在上面一行中对其进行#undef
。但是,将#include
放在列表的底部,以避免意外的后果。
您需要添加:
#undef _MP
在包含Winusb.h
之前。
我相信这是一个MFC特定的问题,MFC将其定义为预处理器符号,唯一的解决方法是明确地不定义它,以便正确地写出结构体。据推测,这有一些其他的副作用,所以请谨慎行事。
实际上,我的错误不是#包括在WinUsb.h之前
我做了一个错误的假设,MFC头文件将最终包括Windows.h,但由于这是一个设备相关的应用程序显然没有。
相关文章:
- 如何编译使用从不同编译器编译的库的应用程序?
- 如何使用CLion和MSVC编译器工具链编译wxWidgets Hello World应用程序而没有错误?
- 是否可以将不同的编译器嵌入到我的应用程序中?
- C++使用较新的编译器构建应用程序,而无需重新构建库
- RVO 何时保证应用/确实适用于 C++20 编译器
- 是否可以在 Azure 应用服务中安装 C++ 编译器
- [expr.unary.op]/9 似乎暗示"运算符!()' 不能应用于下面的类型 A.但编译器不同意这一点
- GCC编译器,为较低版本的GCC编译应用程序
- 如何将我的控制台应用程序链接到使用 clang++ 作为编译器的 OSX 环境中的 /usr/local/lib 中存在
- 如何从我的qt应用程序运行gcc编译器?
- 将 const 限定符应用于模板参数时的不同编译器行为
- 尝试使用VC++编译器构建Qt应用程序:"type_traits":没有这样的文件或目录
- 如何安全地部署使用升级的编译器构建的应用程序
- 不同的输出调用clang上的malloc应用编译器选项-00和-03
- 使用英特尔编译器套件编译 OpenMPI 应用
- CL.exe 构建通用应用程序时编译器和库版本不匹配
- 从沙盒应用程序运行clang编译器的任何可能方式
- c++应用程序找不到com dll,因为编译器使用不正确的guid生成.tlh文件
- 要求编译器内联我的类并应用通常的优化
- 是否有可能使用各种编译器应用程序访问iOS上的文件?