gdipluspath 为 cstddef 和 rpcndr.h 抛出不明确的字节

gdipluspath throws ambiguous byte for cstddef and rpcndr.h

我目前正在更新上次使用 visual studio 2008 编译的古老程序。我正在将它(.lib 项目)更新为 visual studio 2017 以获得最新的 windows sdk( 10.0.15063.0),但是,gdiplus 库会抛出一个不明确的符号错误。 更具体地说:

3>c:\program files (x86)\windows kits\include.0.15063.0\um\GdiplusPath.h(145): error C2872: 'byte': ambiguous symbol
3>c:\program files (x86)\windows kits\include.0.15063.0\shared\rpcndr.h(191): note: could be 'unsigned char byte'
3>C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\VC\Tools\MSVC.11.25503\include\cstddef(15): note: or 'std::byte'

不幸的是,我在这个问题上发现的标准尝试假设歧义错误是我直接造成的,而不是 visual studio 的新包含(我理解的 cstddef 是什么?) .

那么我如何才能将外部库指向使用一个或另一个符号定义?

非常感谢任何帮助。

出现此问题是因为最近的标准引入了 ::std::byte::byte 类型,它们将与 rpcndr.h 中定义的 byte 类型冲突:

// cstddef
enum class byte : unsigned char {};

// rpcndr.h
typedef unsigned char byte;

但这不是 windows headers 的唯一问题,他们还引入了 minmax 与 [=18 冲突的宏(gdiplus 要求) =] 内容.

所以解决方法是仔细控制 windows 和 gdi 加上 headers 的包含方式,如下所示:

//  global compilation flag configuring windows sdk headers
//  preventing inclusion of min and max macros clashing with <limits>
#define NOMINMAX 1

//  override byte to prevent clashes with <cstddef>
#define byte win_byte_override

#include <Windows.h> // gdi plus requires Windows.h
// ...includes for other windows header that may use byte...

//  Define min max macros required by GDI+ headers.
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#else
#error max macro is already defined
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#else
#error min macro is already defined
#endif

#include <gdiplus.h>

//  Undefine min max macros so they won't collide with <limits> header content.
#undef min
#undef max

//  Undefine byte macros so it won't collide with <cstddef> header content.
#undef byte

请注意,此方法意味着用户代码从不使用 windows sdk headers.

中的 byteminmax

此外 byte 可能会与其他 third-party 库发生冲突。

For Visual Studio, this behavior can be turned off by defining the preprocessor value _HAS_STD_BYTE to 0.

取自this article