GerHobbelt / pthread-win32

clone of pthread-win32 (a.k.a. pthreads4w) + local tweaks (including MSVC2008 - MSVC2022 project files)
Other
350 stars 166 forks source link

C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\winbase.h(9531,5): warning C5105: macro expansion producing 'defined' has undefined behavior (compiling source file ......) #14

Closed GerHobbelt closed 3 years ago

GerHobbelt commented 3 years ago

When building the solution using MSVC2019 (tested on Visual Studio 2019, v16.9.5 and latest 16.9.6), every source file outputs this line in the Output window in Visual Studio.

Diagnosis and Fix

Upon investigation, this turns out to be a Microsoft Windows 10 SDK bug:

This is a bug in the Windows SDK which surfaces as a preprocessor warning when compiling in C17 mode, rather than "Legacy Mode", which we switched away from in commit 1357369867810838e6789ee3c964a0edea4564e9

Observe the bug in C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\WinBase.h in this section: note the !defined(_WINBASE_) in there in the result, which is where this preproceessor function WILL NOT WORK:

/*
To turn off/hide the contents of this file:
 #define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS 0
*/

#if !defined(MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS)
#define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS (_WIN32_WINNT >= 0x0502 || !defined(_WINBASE_))
#endif

#if MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS  /* { */

and now here's the patched version, which does the proper thing - a lo, the issue goes away:

/*
To turn off/hide the contents of this file:
 #define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS 0
*/

#if !defined(MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS)
#if (_WIN32_WINNT >= 0x0502 || !defined(_WINBASE_))
#define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS 1
#else
#define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS 0
#endif
#endif

#if MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS  /* { */

Apparently nobody at MS tested the SDK in C17 mode or this should have popped up as soon as you compile anything that includes WinBase.h one way or another. 🤷‍♀️

Filing it here so it can be found. Had a look at the doldrum that's MS bug reporting for devs and I don't like the wall at all:

I get it that you do that kind of thing to keep the loonies away and unload your team, but here's another team that doesn't need the hassle, so filing, patching locally and moving on. Google Search will decide, in the end.

GerHobbelt commented 3 years ago

As stated: patched locally and closing this, since it's not a pthread issue per se, but rather a Windows SDK issue.

(You need local UAC admin rights to patch that SDK WinBase.h file, but I assume you have that or can get it. Anyway, it's obnoxious but not a showstopper and there's no cause for a work-around.)