What's the difference between the WIN32 and _WIN32 defines in C++
WIN32
is a name that you could use and even define in your own code and so might clash with Microsoft's usage. _WIN32
is a name that is reserved for the implementor (in this case Microsoft) because it begins with an underscore and an uppercase letter - you are not allowed to define reserved names in your own code, so there can be no clash.
Which Cross Platform Preprocessor Defines? (__WIN32__ or __WIN32 or WIN32 )?
It depends what you are trying to do. You can check the compiler if your program wants to make use of some specific functions (from the gcc toolchain for example). You can check for operating system ( _WINDOWS
, __unix__
) if you want to use some OS specific functions (regardless of compiler - for example CreateProcess
on Windows and fork
on unix).
Macros for Visual C
Macros for gcc
You must check the documentation of each compiler in order to be able to detect the differences when compiling. I remember that the gnu toolchain(gcc) has some functions in the C library (libc) that are not on other toolchains (like Visual C for example). This way if you want to use those functions out of commodity then you must detect that you are using GCC, so the code you must use would be the following:
#ifdef __GNUC__
// do my gcc specific stuff
#else
// ... handle this for other compilers
#endif
Should I define both _WIN32 and _WIN64 in 64bit build?
_WIN32 doesn't mean what you think it does. It means "I am using the Windows API". The 32 postfix was added back in the days of Windows NT 3.1 to make it distinct from the 16-bit API that was used in Windows version 3. This term has fallen out of favor because of the bitness problem. You can see this at stackoverflow.com, the [win32] tag takes you to [winapi].
Don't remove it, you are using the Windows API.
What's the difference between the WIN32 and _WIN32 defines in C++
WIN32
is a name that you could use and even define in your own code and so might clash with Microsoft's usage. _WIN32
is a name that is reserved for the implementor (in this case Microsoft) because it begins with an underscore and an uppercase letter - you are not allowed to define reserved names in your own code, so there can be no clash.
difference between #if defined(WIN32) and #ifdef(WIN32)
If you use #ifdef syntax, remove the parenthesis.
The difference between the two is that #ifdef
can only use a single condition,
while #if defined(NAME)
can do compound conditionals.
For example in your case:
#if defined(WIN32) && !defined(UNIX)
/* Do windows stuff */
#elif defined(UNIX) && !defined(WIN32)
/* Do linux stuff */
#else
/* Error, both can't be defined or undefined same time */
#endif
Related Topics
How to Fix .Pch File Missing on Build
How to Use Std::Filesystem on Gcc 8
Variadic Template in VS 2012 (Visual C++ November 2012 Ctp)
When to Make a Type Non-Movable in C++11
When to Use Shared_Ptr and When to Use Raw Pointers
Why Does C++ Allow Variable Length Arrays That Aren't Dynamically Allocated
At What Point Does Dereferencing the Null Pointer Become Undefined Behavior
Designated Initializers in C++20
Undefined Reference to 'Pthread_Key_Create' (Linker Error)
Can't C++ Pod Type Have Any Constructor
Heap Corruption While Using Createwindowexw
Compute Objects Moving with Arrows and Mouse
How to Get the Path of a Windows "Special Folder" for a Specific User
Is Circumventing a Class' Constructor Legal or Does It Result in Undefined Behaviour
When Is #Include <New> Library Required in C++
Finding Out the CPU Clock Frequency (Per Core, Per Processor)