Is there a difference between use of _WINDOWS_ and _WIN32 / _WIN64 macros?

Viewed 55

Is there a difference between:

#if defined(_WIN32) || defined(_WIN64)

and:

#ifdef _WINDOWS_

I know that it is necessary to use defined() when there is more than 1 condition.

1 Answers

Yes, there is a difference. The _WIN32 and _WIN64 tokens are defined (conditionally) depending on what platform is being targeted. The former (_WIN32) will be defined for x86, x64, ARM and ARM-64 architectures; the latter (_WIN64) will be defined only for the two 64-bit architectures.

However, the _WINDOWS_ macro will always be defined if the "Windows.h" header file is included by a given source file. It is used (in that header) as a 'guard', to prevent multiple inclusions of its body. A very trimmed-down version of that header is:

/* ...
    Copyright (c) Microsoft Corporation. All rights reserved.
   ...
    Master include file for Windows applications.

--*/

#ifndef _WINDOWS_
#define _WINDOWS_

// ...
// <body of Windows.h>
// ...


#endif /* _WINDOWS_ */

Also, when using the MSVC compiler (and, possibly, some others), the _WIN32 and _WIN64 macros are predefined by the compiler (i.e. no header needs to be included for them to be defined for their relevant target platforms).


Just to add to the confusion, MSVC also adds a _WINDOWS macro (note the lack of the trailing underscore) by default, when creating new projects that target Windows (this is not formally predefined, but is one of the default, per-project "Preprocessor Definitions" in the projects' properties.

Related