C/C++跨平台的的预编译宏

我们在编译一些跨平台的程序的时候难免会遇到_WIN32  __linux__什么之类的SDK或者编译器环境预定义的宏。有很多,之前还分不清_WIN32 和WIN32的区别。不过这里还好有个列表,做个备忘。

 

Please send updates/corrections to predef-contribute.

AIX

TypeMacroDescription
Identification_AIX 
Version_AIX'VR'V = Version
R = 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 VersionMacro
3.2.x_AIX3
_AIX32
4.1_AIX41
4.3_AIX43

Android

TypeMacroFormatDescription
Identification__ANDROID__  
Version__ANDROID_API__VV = API Version

Must 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.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 
Identification__amigaos__Defined by GNU C

Apollo AEGIS

TypeMacro
Identificationaegis

Apollo Domain/OS

TypeMacro
Identificationapollo

Bada

Based on Nucleus OS.

BeOS

TypeMacro
Identification__BEOS__

Blue Gene

TypeMacroDescription
Identification__bg__All Blue Gene systems

Defined by XL C/C++ and GNU C
Version__bgq__Blue Gene/Q

Defined for XL C/C++ and GNU C
Identification__THW_BLUEGENE__All Blue Gene systems

Defined by XL C/C++
Version__TOS_BGQ__Blue Gene/Q

Defined by XL C/C++

BSD Environment

TypeMacroFormatDescription
Identification__FreeBSD__
__NetBSD__
__OpenBSD__
__bsdi__
__DragonFly__
  
VersionBSDYYYYMMYYYY = Year
MM = Month

Must be included from <sys/param.h>
VersionBSD4_2
BSD4_3
BSD4_4
 Must be included from <sys/param.h>
Identification_SYSTYPE_BSD Defined by DEC C
Example
VersionBSDMacro
4.3 Net2199103 
4.4199306BSD4_4
4.4BSD-Lite2199506 

BSD/OS

TypeMacro
Identification__bsdi__

ConvexOS

TypeMacro
Identification__convex__

Cygwin Environment

TypeMacro
Identification__CYGWIN__

DG/UX

TypeMacro
IdentificationDGUX
Identification__DGUX__
Identification__dgux__

DragonFly

TypeMacro
Identification__DragonFly__

DYNIX/ptx

TypeMacro
Identification_SEQUENT_
Identificationsequent

eCos

TypeMacro
Identification__ECOS

EMX Environment

TypeMacro
Identification__EMX__

FreeBSD

TypeMacroFormatDescription
Identification__FreeBSD__  
Identification__FreeBSD_kernel__ From FreeBSD 8.3, 9.1, and 10.0.1
VersionBSD  
Version__FreeBSD__VV = Version
Version__FreeBSD_version?Must be included from <osreldate.h>
Example
FreeBSD__FreeBSD____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
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.

TypeMacro
Identification__FreeBSD_kernel__ && __GLIBC__

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

GNU/Linux

TypeMacro
Identification__gnu_linux__

HI-UX MPP

TypeMacro
Identification__hiuxmpp

HP-UX

TypeMacroDescription
Identification_hpuxDefined by HP UPC
Identificationhpux 
Identification__hpux 

IBM OS/400

TypeMacro
Identification__OS400__

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
Identification__linux__1
IdentificationlinuxObsolete (not POSIX compliant)
Identification__linuxObsolete (not POSIX compliant)

LynxOS

TypeMacro
Identification__Lynx__

MacOS

TypeMacroDescription
IdentificationmacintoshMac OS 9
IdentificationMacintoshMac OS 9
Identification__APPLE__ && __MACH__Mac OS X

Defined 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
Identification__MORPHOS__

MPE/iX

TypeMacro
Identificationmpeix
Identification__mpexl

MSDOS

TypeMacro
IdentificationMSDOS
Identification__MSDOS__
Identification_MSDOS
Identification__DOS__

NetBSD

TypeMacroFormatDescription
Identification__NetBSD__  
VersionBSD  
VersionNetBSD'V'_'R' V = Version
R = Revision

Must be included from <sys/param.h>
Version__NetBSD_Version__VVRRAAPP00VV = Version
RR = Revision
AA = Release
PP = Patch

From NetBSD 1.2D (?) until NetBSD 2.0H

Must be included from <sys/param.h>
Version__NetBSD_Version__VVRR00PP00VV = Version
RR = Revision
PP = Patch

From NetBSD 2.99.9

Must 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.2D102040000 
1.2.1102000100 

NonStop

TypeMacro
Identification__TANDEM

Nucleus RTOS

TypeMacro
Identification__nucleus__

OpenBSD

TypeMacroFormatDescription
Identification__OpenBSD__  
VersionBSD  
VersionOpenBSD'V'_'R' V = Version
R = Revision

Must be included from <sys/param.h>
Example
OpenBSDMacro
3.1OpenBSD3_1
3.9OpenBSD3_9

OS/2

TypeMacro
IdentificationOS2
Identification_OS2
Identification__OS2__
Identification__TOS_OS2__

Palm OS

TypeMacroDescription
Identification__palmos__Defined by GNU C in PRC-Tools

Plan 9

TypeMacro
IdentificationEPLAN9

Pyramid DC/OSx

TypeMacro
Identificationpyr

QNX

TypeMacroFormatDescription
Identification__QNX__ QNX 4.x
Identification__QNXNTO__ QNX 6.x
Version_NTO_VERSIONVRRV = Version
RR = Revision

Only available when __QNXNTO__ is defined.

Must be included from <sys/neutrino.h>
VersionBBNDK_VERSION_CURRENTVVRRRRPPPPV = Version
RRRR = Revision
PPPP = Patch

Only available on Blackberry 10

From Blackberry 10.1.0

Must 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 -s
Version = uname -r
Any 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
Identification__VOS__  
Version__VOS__VV = 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
Identification__sysv__ 
Identification__SVR4 
Identification__svr4__ 
Identification_SYSTYPE_SVR4Defined on IRIX

Syllable

TypeMacro
Identification__SYLLABLE__

Symbian OS

TypeMacro
Identification__SYMBIAN32__

Tru64 (OSF/1)

TypeMacro
Identification__osf__
Identification__osf

Ultrix

TypeMacro
Identificationultrix
Identification__ultrix
Identification__ultrix__
Identificationunix & vax

UNICOS

TypeMacroFormatDescription
Identification_UNICOS  
Version_UNICOSVV = Version

UNICOS/mp

TypeMacroDescription
Identification_CRAY
__crayx1
 

UNIX Environment

TypeMacro
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

TypeMacro
Identificationsco
Identification_UNIXWARE7

U/Win Environment

TypeMacro
Identification_UWIN

VMS

TypeMacroFormatDescription
IdentificationVMS  
Identification__VMS  
Version__VMS_VERVVRREPPTTVV = Version
RR = Revision
E = Edit number
PP = Patch (01 = A, ... 26 = Z)
TT = Type (22 = official)
Example
VMS__VMS_VER
6.160100022
6.260200022
6.2-1I60210922

VxWorks

TypeMacroDescription 
Identification__VXWORKS__Defined by GNU C and Diab (from ?) 
Identification__vxworksDefined by GNU C and Diab (from ?) 
Version_WRS_VXWORKS_MAJORVersion

Must be included from <version.h>
 
Version_WRS_VXWORKS_MINORRevision

Must be included from <version.h>
 
Version_WRS_VXWORKS_MAINTPatch/maintenance

Must be included from <version.h>
 
Mode__RTP__For 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
Identification__WIN32__Defined by Borland C++
Identification__TOS_WIN__Defined by xlC
Identification__WINDOWS__Defined by Watcom C/C++

Windows CE

TypeMacroFormatDescription
Identification_WIN32_WCE Defined by Embedded Visual Studio C++
Version_WIN32_WCEVRRV = Version
R = Revision
IdentificationWIN32_PLATFORM_'P' P = Platform
VersionWIN32_PLATFORM_'P'VP = Platform
V = 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 = Version
RR = Revision
PP = Patch
Example
Wind/U_WINDU_SOURCE
3.1.20x030102

z/OS

TypeMacroDescription
Identification__MVS__Host
Identification__HOS_MVS__Host
Identification__TOS_MVS__Target

Related

 

 

references:

http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor

http://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html

http://sourceforge.net/p/predef/wiki/OperatingSystems/

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值