gdipluspath throws ambiguous byte for cstddef and rpcndr.h

*爱你&永不变心* 提交于 2019-12-08 03:25:34

问题


I am currently updating an ancient program which was last compiled with visual studio 2008. I am updating it (.lib project) to visual studio 2017 for the newest windows sdk (10.0.15063.0), however, the gdiplus library throws an ambiguous symbol error. More specifically:

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

The standard attempts that I have found at this issue unfortunately suppose that the ambiguity error is made by me directly, and not by a new inclusion by visual studio (Which is what I understand cstddef to be?).

So how can I point an external library towards using one symbol definition or the other?

Any help is greatly appreciated.


回答1:


This problem occurs because recent standard introduced ::std::byte and ::byte types which will clash with byte type defined in rpcndr.h:

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

// rpcndr.h
typedef unsigned char byte;

But that is not the only problem with windows headers, they also introduce min and max macros (required by gdiplus) that clash with <limits> content.

So the workaround would be carefully control how windows and gdi plus headers are included, like this:

//  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

Note that this approach implies that user code never uses byte, min and max from windows sdk headers.

Also byte may clash with other third-party libraries.




回答2:


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

taken from this article.



来源:https://stackoverflow.com/questions/45957830/gdipluspath-throws-ambiguous-byte-for-cstddef-and-rpcndr-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!