Files
lixiyu-net/data/posts/2023-11-05-platform-macros.md
2023-12-25 17:21:39 +08:00

31 KiB

title, tags, time, summary
title tags time summary
C/C++ Cross-Platform Compile-Macros
C
C++
2023-11-05 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

Type Macro Description
Identification _AIX
Version _AIX'VR' V = VersionR = Revision
Identification TOS_AIX Defined 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 Version Macro
3.2.x _AIX3_AIX32
4.1 _AIX41
4.3 _AIX43

Android

Type Macro Format Description
Identification ANDROID
Version ANDROID_API V V = 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 Version ANDROID_API
1.0 1
1.1 2
1.5 3
1.6 4
2.0 5
2.0.1 6
2.1 7
2.2 8
2.3 9
2.3.3 10
3.0 11

Amdahl UTS

Type Macro
Identification UTS

AmigaOS

Type Macro Description
Identification AMIGA
Identification amigaos Defined by GNU C

Apollo AEGIS

Type Macro
Identification aegis

Apollo Domain/OS

Type Macro
Identification apollo

Bada
Based on Nucleus OS.

BeOS

Type Macro
Identification BEOS

Blue Gene

Type Macro Description
Identification bg All Blue Gene systemsDefined by XL C/C++ and GNU C
Version bgq Blue Gene/QDefined for XL C/C++ and GNU C
Identification THW_BLUEGENE All Blue Gene systemsDefined by XL C/C++
Version TOS_BGQ Blue Gene/QDefined by XL C/C++

BSD Environment

Type Macro Format Description
Identification FreeBSD__NetBSD__OpenBSD__bsdi__DragonFly
Version BSD YYYYMM YYYY = YearMM = MonthMust be included from <sys/param.h>
Version BSD4_2BSD4_3BSD4_4 Must be included from <sys/param.h>
Identification _SYSTYPE_BSD Defined by DEC C

Example

Version BSD Macro
4.3 Net2 199103
4.4 199306 BSD4_4
4.4BSD-Lite2 199506

BSD/OS

Type Macro
Identification bsdi

ConvexOS

Type Macro
Identification convex

Cygwin Environment

Type Macro
Identification CYGWIN

DG/UX

Type Macro
Identification DGUX
Identification DGUX
Identification dgux

DragonFly

Type Macro
Identification DragonFly

DYNIX/ptx

Type Macro
Identification SEQUENT
Identification sequent

eCos

Type Macro
Identification __ECOS

EMX Environment

Type Macro
Identification EMX

FreeBSD

Type Macro Format Description
Identification FreeBSD
Identification FreeBSD_kernel From FreeBSD 8.3, 9.1, and 10.0.1
Version BSD
Version FreeBSD V V = Version
Version __FreeBSD_version ? Must be included from osreldate.h

Example

FreeBSD FreeBSD __FreeBSD_version
1.x 1
2.0-RELEASE 2 119411
2.2-RELEASE 2 220000
3.0-RELEASE 3 300005
4.0-RELEASE 4 400017
4.5-RELEASE 4 450000

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.

Type Macro
Identification GNU 1
Identification gnu_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.

Type Macro
Identification FreeBSD_kernel && GLIBC

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

Type Macro
Identification gnu_linux

HI-UX MPP

Type Macro
Identification __hiuxmpp

HP-UX

Type Macro Description
Identification _hpux Defined by HP UPC
Identification hpux
Identification __hpux

IBM OS/400

Type Macro
Identification OS400

INTEGRITY

Type Macro
Identification __INTEGRITY

Interix Environment

Type Macro Description
Identification __INTERIX Defined by GNU C and Visual Studio

IRIX

Type Macro
Identification sgi
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

Type Macro Description
Identification linux 1
Identification linux Obsolete (not POSIX compliant)
Identification __linux Obsolete (not POSIX compliant)

LynxOS

Type Macro
Identification Lynx

MacOS

Type Macro Description
Identification macintosh Mac OS 9
Identification Macintosh Mac OS 9
Identification APPLE && MACH Mac OS XDefined by GNU C and Intel C++

Microware OS-9

Type Macro Description
Identification __OS9000 Defined by Ultimate C/C++
Identification _OSK Defined by Ultimate C/C++

MINIX

Type Macro
Identification __minix

MorphOS

Type Macro
Identification MORPHOS

MPE/iX

Type Macro
Identification mpeix
Identification __mpexl

MSDOS

Type Macro
Identification MSDOS
Identification MSDOS
Identification _MSDOS
Identification DOS

NetBSD

Type Macro Format Description
Identification NetBSD
Version BSD
Version NetBSD'V'_'R' V = VersionR = RevisionMust be included from <sys/param.h>
Version NetBSD_Version VVRRAAPP00 VV = VersionRR = RevisionAA = ReleasePP = PatchFrom NetBSD 1.2D (?) until NetBSD 2.0HMust be included from <sys/param.h>
Version NetBSD_Version VVRR00PP00 VV = VersionRR = RevisionPP = PatchFrom NetBSD 2.99.9Must be included from <sys/param.h>

Example

NetBSD NetBSD_Version Macro
0.8 NetBSD0_8
0.9 NetBSD0_9
1.0 NetBSD1_0 = 1
1.0A NetBSD1_0 = 2
1.2D 102040000
1.2.1 102000100

NonStop

Type Macro
Identification __TANDEM

Nucleus RTOS

Type Macro
Identification nucleus

OpenBSD

Type Macro Format Description
Identification OpenBSD
Version BSD
Version OpenBSD'V'_'R' V = VersionR = RevisionMust be included from sys/param.h

Example

OpenBSD Macro
3.1 OpenBSD3_1
3.9 OpenBSD3_9

OS/2

Type Macro
Identification OS2
Identification _OS2
Identification OS2
Identification TOS_OS2

Palm OS

Type Macro Description
Identification palmos Defined by GNU C in PRC-Tools

Plan 9

Type Macro
Identification EPLAN9

Pyramid DC/OSx

Type Macro
Identification pyr

QNX

Type Macro Format Description
Identification QNX QNX 4.x
Identification QNXNTO QNX 6.x
Version _NTO_VERSION VRR V = VersionRR = RevisionOnly available when QNXNTO is defined.Must be included from sys/neutrino.h/
Version BBNDK_VERSION_CURRENT VVRRRRPPPP V = VersionRRRR = RevisionPPPP = PatchOnly available on Blackberry 10From Blackberry 10.1.0Must be included from bbndk.h

Example

QNX _NTO_VERSION
6.2 620

Reliant UNIX

Type Macro
Identification sinux

SCO OpenServer

Type Macro Description
Identification M_I386 Defined by GNU C
Identification M_XENIX Defined by GNU C
Identification _SCO_DS

Solaris

Type Macro Description
Identification sun
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

Solaris Macro
2.7 __SunOS_5_7
8 __SunOS_5_8

Stratus VOS

Type Macro Format Description
Identification VOS
Version VOS V V = 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

Type Macro Description
Identification sysv
Identification __SVR4
Identification svr4
Identification _SYSTYPE_SVR4 Defined on IRIX

Syllable

Type Macro
Identification SYLLABLE

Symbian OS

Type Macro
Identification SYMBIAN32

Tru64 (OSF/1)

Type Macro
Identification osf
Identification __osf

Ultrix

Type Macro
Identification ultrix
Identification __ultrix
Identification ultrix
Identification unix & vax

UNICOS

Type Macro Format Description
Identification _UNICOS
Version _UNICOS V V = Version

UNICOS/mp

Type Macro Description
Identification _CRAY__crayx1

UNIX Environment

Type Macro
Identification unix
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

Type Macro
Identification sco
Identification _UNIXWARE7

U/Win Environment

Type Macro
Identification _UWIN

VMS

Type Macro Format Description
Identification VMS
Identification __VMS
Version __VMS_VER VVRREPPTT VV = VersionRR = RevisionE = Edit numberPP = Patch (01 = A, ... 26 = Z)TT = Type (22 = official)

Example

VMS __VMS_VER
6.1 60100022
6.2 60200022
6.2-1I 60210922

VxWorks

Type Macro Description
Identification VXWORKS Defined by GNU C and Diab (from ?)
Identification __vxworks Defined by GNU C and Diab (from ?)
Version _WRS_VXWORKS_MAJOR VersionMust be included from version.h
Version _WRS_VXWORKS_MINOR RevisionMust be included from version.h
Version _WRS_VXWORKS_MAINT Patch/maintenanceMust be included from version.h
Mode RTP For real-time mode
Mode _WRS_KERNEL For kernel mode

Example

VxWorks _WRS_VXWORKS_MAJOR _WRS_VXWORKS_MINOR _WRS_VXWORKS_MAINT
6.2 6 2 0

Windows

Type Macro Description
Identification _WIN16 Defined for 16-bit environments 1
Identification _WIN32 Defined for both 32-bit and 64-bit environments 1
Identification _WIN64 Defined for 64-bit environments 1
Identification WIN32 Defined by Borland C++
Identification TOS_WIN Defined by xlC
Identification WINDOWS Defined by Watcom C/C++

Windows CE

Type Macro Format Description
Identification _WIN32_WCE Defined by Embedded Visual Studio C++
Version _WIN32_WCE VRR V = VersionR = Revision
Identification WIN32PLATFORM'P' P = Platform
Version WIN32PLATFORM'P' V P = PlatformV = Version

Example

Version _WIN32_WCE
2.01 201
2.11 211
3.0 300
4.0 400
4.1 410
4.2 420
5.0 501
Platform Macro Value
H/PC 2000 WIN32_PLATFORM_HPC2000
H/PC Pro 2.11 WIN32_PLATFORM_HPCPRO 211
H/PC Pro 3.0 WIN32_PLATFORM_HPCPRO 300
Pocket PC WIN32_PLATFORM_PSPC 1
Pocket PC 2002 WIN32_PLATFORM_PSPC 310
Windows Mobile 2003 WIN32_PLATFORM_PSPC 400
Smartphone 2002 WIN32_PLATFORM_WFSP 100

Wind/U Environment

Type Macro Format Description
Identification _WINDU_SOURCE
Version _WINDU_SOURCE 0xVVRRPP VV = VersionRR = RevisionPP = Patch

Example

Wind/U _WINDU_SOURCE
3.1.2 0x030102

z/OS

Type Macro Description
Identification MVS Host
Identification HOS_MVS Host
Identification TOS_MVS Target