Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the WIN32 and _WIN32 defines in C++

I know that WIN32 denotes win32 compilation but what is _WIN32 used for?

like image 615
Adam Naylor Avatar asked Mar 19 '09 12:03

Adam Naylor


People also ask

Where is _WIN64 defined?

_WIN64 Defined as 1 when the compilation target is 64-bit ARM or x64. Otherwise, undefined. _WINRT_DLL Defined as 1 when compiled as C++ and both /ZW (Windows Runtime Compilation) and /LD or /LDd compiler options are set. Otherwise, undefined.

What is dwin32?

(1) A 32-bit version of Windows. Many editions of Windows have come in both 32-bit and 64-bit versions. See 32-bit computing and x86/x64. (2) Win32 is the programming interface (API) for 32-bit and 64-bit Windows operating systems.


1 Answers

To elaborate (Neil Butterworth and blue.tuxedo have already given the correct answer):

  • WIN32 is defined by the SDK or the build environment, so it does not use the implementation reserved namespace
  • _WIN32 is defined by the compiler so it uses the underscore to place it in the implementation-reserved namespace

You'll find a similar set of dual defines with nearly identical names and similar uses such as _UNICODE/UNICODE, _DEBUG/DEBUG, or maybe _DLL/DLL (I think that only the UNICODE ones get much of any use in their different versions). Though sometimes in these cases (like _UNICODE), instead of the underscore version being defined by the compiler, they are used to control what the CRT headers do:

  • _UNICODE tells the CRT headers that CRT names which can be either Unicode or ANSI (such as _tcslen() should map to the wide character variant (wcslen())
  • UNICODE does something similar for the SDK (maps Win32 APIs to their "W" variants)

Essentially the versions with the underscore are controlled by or used by the compiler team, the versions without the underscore are controlled/used by teams outside of the compiler. Of course, there's probably going to be a lot overlap due to compatibility with past versions and just general mistakes by one team or the other.

I find it confusing as hell - and find that they are used nearly interchangeably in user code (usually, when you see one defined, you'll see the other defined in the same place, because if you need one you need the other). Personally, I think that you should use the versions without the underscore (unless you're writing the compiler's runtime) and make sure they both get defined (whether via hearers or compiler switches as appropriate) when you're defining one.


Note that the SDK will define _WIN32 when building for the Mac because the compiler doesn't, kind of overstepping it's bounds. I'm not sure what projects use a Win32 API an a compiler targeting the Mac - maybe some version of Office for the Max or something.

like image 191
Michael Burr Avatar answered Sep 30 '22 09:09

Michael Burr