C/C++ Cross-Platform Compile-Macros
When we compile some cross -platform programs, we will inevitably encounter _win32 __linux__ what is the macro of the compiler or the compiler environment.It indicates some information of the current platform environment to the compiler.
When we compile some cross -platform programs, we will inevitably encounter _win32 , Linux What are the macroscopic macro of the compiler environment.There are many differences between _win32 and win32 before.But there is a list here, making a memo.
For example, a code that can only be compiled under the Unix-Like platform in a set.If you compile the wrong error on the non-Unix-Like platform, then my code can add a macro to check whether it is a UNIX environment.If it is normally compiled, the error is thrown directly.
#ifndef __unix__
#error This program should be complied and work in UNIX-LIKE platform.
#endif
The complete code is as follows:
#ifndef __unix__
#error This program should be complied and work in UNIX-LIKE platform.
#endif
#include <stdio.h>
int main()
{
printf("this is unix-like platform");
return 0;
}
This code can be compiled normally in the Mac OS and Linux environments, and an error will be reported under Windows.
Below is a macro list of a detection environment.
Please send updates/corrections to predef-contribute.
Example
If _AIX
is defined, then the following macros can be used to determine the version. Notice that the macros indicates the mentioned version or higher. For example, if _AIX43
is defined, then _AIX41
will also be defined.
Notice that Android is based on Linux, and that the Linux macros also are defined for Android.
Example
Bada
Based on Nucleus OS.
Example
Example
For more information see the FreeBSD porters handbook.
GNU aka GNU/Hurd
The official name of this operating system is GNU. Hurd is the kernel in the GNU operating system. It is often listed as GNU/Hurd since there is also GNU/Linux and GNU/kFreeBSD, which are most of the GNU operating system with the Linux and FreeBSD kernels respectively.
GNU/kFreeBSD
GNU/kFreeBSD is one of the Debian distros that is based on the FreeBSD kernel rather than the Linux or Hurd kernels.
Notice that FreeBSD also defines __FreeBSD_kernel__
so the __GLIBC__
macro must be checked to distinguish it.
GNU/Linux
Linux kernel
Systems based on the Linux kernel define these macros. There are two major Linux-based operating systems: GNU/Linux andAndroid, and numerous others like Ångström or OpenEmbedded
Example
Example
Example
Use the SVR4 macros to distinguish between Solaris and SunOS.
#if defined(sun) || defined(**sun) # if defined(**SVR4) || defined(svr4) /_ Solaris / # else / SunOS _/ # endif #endif
Example
Notice that the __VOS__
macro is defined by the compiler, but as several compilers can co-exist in the same OS release, the version number is not reliable.
SVR4 Environment
Notice that not all compilers defines these macros, e.g. the xlC or the DEC C/C++ compiler, so it may be better to use the POSIX or X/Open standard macros instead.
UnixWare
Example
Example
Example
Example