2023, November, 05
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.

AIX

TypeMacroDescription
Identification_AIX
Version_AIX'VR'V = VersionR = Revision
IdentificationTOS_AIXDefined by xlC

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.

AIX VersionMacro
3.2.x_AIX3_AIX32
4.1_AIX41
4.3_AIX43

Android

TypeMacroFormatDescription
IdentificationANDROID
VersionANDROID_APIVV = API VersionMust be included from <android/api-level.h>

Notice that Android is based on Linux, and that the Linux macros also are defined for Android.
Example

Android VersionANDROID_API
1.01
1.12
1.53
1.64
2.05
2.0.16
2.17
2.28
2.39
2.3.310
3.011

Amdahl UTS

TypeMacro
IdentificationUTS

AmigaOS

TypeMacroDescription
IdentificationAMIGA
IdentificationamigaosDefined by GNU C

Apollo AEGIS

TypeMacro
Identificationaegis

Apollo Domain/OS

TypeMacro
Identificationapollo

Bada
Based on Nucleus OS.

BeOS

TypeMacro
IdentificationBEOS

Blue Gene

TypeMacroDescription
IdentificationbgAll Blue Gene systemsDefined by XL C/C++ and GNU C
VersionbgqBlue Gene/QDefined for XL C/C++ and GNU C
IdentificationTHW_BLUEGENEAll Blue Gene systemsDefined by XL C/C++
VersionTOS_BGQBlue Gene/QDefined by XL C/C++

BSD Environment

TypeMacroFormatDescription
IdentificationFreeBSD__NetBSD__OpenBSD__bsdi__DragonFly
VersionBSDYYYYMMYYYY = YearMM = MonthMust be included from <sys/param.h>
VersionBSD4_2BSD4_3BSD4_4Must be included from <sys/param.h>
Identification_SYSTYPE_BSDDefined by DEC C

Example

VersionBSDMacro
4.3 Net2199103
4.4199306BSD4_4
4.4BSD-Lite2199506

BSD/OS

TypeMacro
Identificationbsdi

ConvexOS

TypeMacro
Identificationconvex

Cygwin Environment

TypeMacro
IdentificationCYGWIN

DG/UX

TypeMacro
IdentificationDGUX
IdentificationDGUX
Identificationdgux

DragonFly

TypeMacro
IdentificationDragonFly

DYNIX/ptx

TypeMacro
IdentificationSEQUENT
Identificationsequent

eCos

TypeMacro
Identification__ECOS

EMX Environment

TypeMacro
IdentificationEMX

FreeBSD

TypeMacroFormatDescription
IdentificationFreeBSD
IdentificationFreeBSD_kernelFrom FreeBSD 8.3, 9.1, and 10.0.1
VersionBSD
VersionFreeBSDVV = Version
Version__FreeBSD_version?Must be included from osreldate.h

Example

FreeBSDFreeBSD__FreeBSD_version
1.x1
2.0-RELEASE2119411
2.2-RELEASE2220000
3.0-RELEASE3300005
4.0-RELEASE4400017
4.5-RELEASE4450000

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.

TypeMacro
IdentificationGNU 1
Identificationgnu_hurd 1

GNU/kFreeBSD
GNU/kFreeBSD is one of the Debian distros that is based on the FreeBSD kernel rather than the Linux or Hurd kernels.

TypeMacro
IdentificationFreeBSD_kernel && GLIBC

Notice that FreeBSD also defines __FreeBSD_kernel__ so the __GLIBC__ macro must be checked to distinguish it.
GNU/Linux

TypeMacro
Identificationgnu_linux

HI-UX MPP

TypeMacro
Identification__hiuxmpp

HP-UX

TypeMacroDescription
Identification_hpuxDefined by HP UPC
Identificationhpux
Identification__hpux

IBM OS/400

TypeMacro
IdentificationOS400

INTEGRITY

TypeMacro
Identification__INTEGRITY

Interix Environment

TypeMacroDescription
Identification__INTERIXDefined by GNU C and Visual Studio

IRIX

TypeMacro
Identificationsgi
Identification__sgi

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

TypeMacroDescription
Identificationlinux1
IdentificationlinuxObsolete (not POSIX compliant)
Identification__linuxObsolete (not POSIX compliant)

LynxOS

TypeMacro
IdentificationLynx

MacOS

TypeMacroDescription
IdentificationmacintoshMac OS 9
IdentificationMacintoshMac OS 9
IdentificationAPPLE && MACHMac OS XDefined by GNU C and Intel C++

Microware OS-9

TypeMacroDescription
Identification__OS9000Defined by Ultimate C/C++
Identification_OSKDefined by Ultimate C/C++

MINIX

TypeMacro
Identification__minix

MorphOS

TypeMacro
IdentificationMORPHOS

MPE/iX

TypeMacro
Identificationmpeix
Identification__mpexl

MSDOS

TypeMacro
IdentificationMSDOS
IdentificationMSDOS
Identification_MSDOS
IdentificationDOS

NetBSD

TypeMacroFormatDescription
IdentificationNetBSD
VersionBSD
VersionNetBSD'V'_'R'V = VersionR = RevisionMust be included from <sys/param.h>
VersionNetBSD_VersionVVRRAAPP00VV = VersionRR = RevisionAA = ReleasePP = PatchFrom NetBSD 1.2D (?) until NetBSD 2.0HMust be included from <sys/param.h>
VersionNetBSD_VersionVVRR00PP00VV = VersionRR = RevisionPP = PatchFrom NetBSD 2.99.9Must be included from <sys/param.h>

Example

NetBSDNetBSD_VersionMacro
0.8NetBSD0_8
0.9NetBSD0_9
1.0NetBSD1_0 = 1
1.0ANetBSD1_0 = 2
1.2D102040000
1.2.1102000100

NonStop

TypeMacro
Identification__TANDEM

Nucleus RTOS

TypeMacro
Identificationnucleus

OpenBSD

TypeMacroFormatDescription
IdentificationOpenBSD
VersionBSD
VersionOpenBSD'V'_'R'V = VersionR = RevisionMust be included from sys/param.h

Example

OpenBSDMacro
3.1OpenBSD3_1
3.9OpenBSD3_9

OS/2

TypeMacro
IdentificationOS2
Identification_OS2
IdentificationOS2
IdentificationTOS_OS2

Palm OS

TypeMacroDescription
IdentificationpalmosDefined by GNU C in PRC-Tools

Plan 9

TypeMacro
IdentificationEPLAN9

Pyramid DC/OSx

TypeMacro
Identificationpyr

QNX

TypeMacroFormatDescription
IdentificationQNXQNX 4.x
IdentificationQNXNTOQNX 6.x
Version_NTO_VERSIONVRRV = VersionRR = RevisionOnly available when QNXNTO is defined.Must be included from sys/neutrino.h/
VersionBBNDK_VERSION_CURRENTVVRRRRPPPPV = VersionRRRR = RevisionPPPP = PatchOnly available on Blackberry 10From Blackberry 10.1.0Must be included from bbndk.h

Example

QNX_NTO_VERSION
6.2620

Reliant UNIX

TypeMacro
Identificationsinux

SCO OpenServer

TypeMacroDescription
IdentificationM_I386Defined by GNU C
IdentificationM_XENIXDefined by GNU C
Identification_SCO_DS

Solaris

TypeMacroDescription
Identificationsun
Identification__sun
Version_'System''Version'System = uname -sVersion = uname -rAny illegal character is replaced by an underscore.Defined by Sun Studio

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

SolarisMacro
2.7__SunOS_5_7
8__SunOS_5_8

Stratus VOS

TypeMacroFormatDescription
IdentificationVOS
VersionVOSVV = Version

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

TypeMacroDescription
Identificationsysv
Identification__SVR4
Identificationsvr4
Identification_SYSTYPE_SVR4Defined on IRIX

Syllable

TypeMacro
IdentificationSYLLABLE

Symbian OS

TypeMacro
IdentificationSYMBIAN32

Tru64 (OSF/1)

TypeMacro
Identificationosf
Identification__osf

Ultrix

TypeMacro
Identificationultrix
Identification__ultrix
Identificationultrix
Identificationunix & vax

UNICOS

TypeMacroFormatDescription
Identification_UNICOS
Version_UNICOSVV = Version

UNICOS/mp

TypeMacroDescription
Identification_CRAY__crayx1

UNIX Environment

TypeMacro
Identificationunix
Identification__unix

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

TypeMacro
Identificationsco
Identification_UNIXWARE7

U/Win Environment

TypeMacro
Identification_UWIN

VMS

TypeMacroFormatDescription
IdentificationVMS
Identification__VMS
Version__VMS_VERVVRREPPTTVV = VersionRR = RevisionE = Edit numberPP = Patch (01 = A, ... 26 = Z)TT = Type (22 = official)

Example

VMS__VMS_VER
6.160100022
6.260200022
6.2-1I60210922

VxWorks

TypeMacroDescription
IdentificationVXWORKSDefined by GNU C and Diab (from ?)
Identification__vxworksDefined by GNU C and Diab (from ?)
Version_WRS_VXWORKS_MAJORVersionMust be included from version.h
Version_WRS_VXWORKS_MINORRevisionMust be included from version.h
Version_WRS_VXWORKS_MAINTPatch/maintenanceMust be included from version.h
ModeRTPFor real-time mode
Mode_WRS_KERNELFor kernel mode

Example

VxWorks_WRS_VXWORKS_MAJOR_WRS_VXWORKS_MINOR_WRS_VXWORKS_MAINT
6.2620

Windows

TypeMacroDescription
Identification_WIN16Defined for 16-bit environments 1
Identification_WIN32Defined for both 32-bit and 64-bit environments 1
Identification_WIN64Defined for 64-bit environments 1
IdentificationWIN32Defined by Borland C++
IdentificationTOS_WINDefined by xlC
IdentificationWINDOWSDefined by Watcom C/C++

Windows CE

TypeMacroFormatDescription
Identification_WIN32_WCEDefined by Embedded Visual Studio C++
Version_WIN32_WCEVRRV = VersionR = Revision
IdentificationWIN32PLATFORM'P'P = Platform
VersionWIN32PLATFORM'P'VP = PlatformV = Version

Example

Version_WIN32_WCE
2.01201
2.11211
3.0300
4.0400
4.1410
4.2420
5.0501
PlatformMacroValue
H/PC 2000WIN32_PLATFORM_HPC2000
H/PC Pro 2.11WIN32_PLATFORM_HPCPRO211
H/PC Pro 3.0WIN32_PLATFORM_HPCPRO300
Pocket PCWIN32_PLATFORM_PSPC1
Pocket PC 2002WIN32_PLATFORM_PSPC310
Windows Mobile 2003WIN32_PLATFORM_PSPC400
Smartphone 2002WIN32_PLATFORM_WFSP100

Wind/U Environment

TypeMacroFormatDescription
Identification_WINDU_SOURCE
Version_WINDU_SOURCE0xVVRRPPVV = VersionRR = RevisionPP = Patch

Example

Wind/U_WINDU_SOURCE
3.1.20x030102

z/OS

TypeMacroDescription
IdentificationMVSHost
IdentificationHOS_MVSHost
IdentificationTOS_MVSTarget