编译错误:rpcndr.h(192,14): error C2872: “byte”: 不明确的符号
迪丽瓦拉
2024-05-30 18:36:27
0

Windows上使用C++11 编译出现错误

1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\rpcndr.h(192,14): error C2872: “byte”: 不明确的符号

1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\rpcndr.h(191,23): message : 可能是“unsigned char byte”

1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.28.29910\include\cstddef(29,12): message : 或 “std::byte”

原因

C++17 内部定义了std:byte类型,Windows系统定义了byte类型,两个类型冲突了。并且在rpcndr.h头文件内部使用了byte,编译器不知道该使用哪个byte。rpcndr.h在引入 Windows.h时会导入。

解决办法

1,不使用 using namespace std; ,这样两个类型在不同的命名空间,因此不会冲突。

2. 全局定义 宏 _HAS_STD_BYTE=0 ,这个会禁用std::byte类型。弊端:如果外部库使用了std::byte可以能会导致无法编译。

3. 提前包含 #include ,Windows.h内部会导入rpcndr.h。

以下是报错代码,报错:rpcndr.h(192,14): error C2872: “byte”: 不明确的符号

#include  // for std::byte
using namespace std;
#include 

但以下代码不会报错

#include  // for std::byte
#include 
using namespace std;
#include   // 这个windows.h被上一个屏蔽了

因为使用std命名空间以前以及包含了 Windows.h,而第二个Windows.h被第一个Windows.h屏蔽了不起作用,所以不过会报错。

如果项目中有一个“总同头文件”或“预处理头文件”,在里面添加#include 就可以避免byte类型冲突。顺便可以加上以下宏,消除部分windows.h内部的宏:

    #ifndef NOMINMAX#define NOMINMAX 1// prevent windows redefining min/max#endif#include #undef far#undef near

相关内容