C/C++跨平台的的预编译宏
我们在编译一些跨平台的程序的时候难免会遇到_WIN32 __linux__什么之类的SDK或者编译器环境预定义的宏。有很多,之前还分不清_WIN32 和WIN32的区别。不过这里还好有个列表,做个备忘。
Please send updates/corrections to predef-contribute.
AIX
| Type | Macro | Description |
|---|---|---|
| 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 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 Version
Must be included from |
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 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
| Type | Macro | Format | Description |
|---|---|---|---|
| Identification | __FreeBSD____NetBSD____OpenBSD____bsdi____DragonFly__ |
||
| Version | BSD |
YYYYMM | YYYY = Year MM = Month Must be included from |
| 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 X
Defined 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 = Version R = Revision Must be included from |
|
| Version | __NetBSD_Version__ |
VVRRAAPP00 | VV = Version RR = Revision AA = Release PP = Patch From NetBSD 1.2D (?) until NetBSD 2.0H Must be included from |
| Version | __NetBSD_Version__ |
VVRR00PP00 | VV = Version RR = Revision PP = Patch From NetBSD 2.99.9 Must be included from |
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 = Version R = Revision Must be included from |
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 = Version RR = Revision Only available when Must be included from |
| Version | BBNDK_VERSION_CURRENT |
VVRRRRPPPP | V = Version RRRR = Revision PPPP = Patch Only available on Blackberry 10 From Blackberry 10.1.0 Must be included from |
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 = Version RR = Revision E = Edit number PP = 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 |
Version
Must be included from |
|
| Version | _WRS_VXWORKS_MINOR |
Revision
Must be included from |
|
| Version | _WRS_VXWORKS_MAINT |
Patch/maintenance
Must be included from |
|
| 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 = Version R = Revision |
| Identification | WIN32_PLATFORM_'P' |
P = Platform | |
| Version | WIN32_PLATFORM_'P' |
V | P = Platform V = 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 = Version RR = Revision PP = 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 |
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/
C/C++跨平台的的预编译宏的更多相关文章
- linux, windows, mac, ios等平台GCC预编译宏判断
写跨平台c/c++程序的时候,需要搞清各平台下面的预编译宏,区分各平台代码.而跨平台c/c++编程,GCC基本在各平台都可以使用.整理了一份各平台预编译宏的判断示例. 需要注意几点: * window ...
- GCC预编译宏查看
编译调试代码时,总是遇到要使用编译器预编译宏进行跨平台编译. gcc -E -dM -</dev/null 编译器版本 Thread model: posix gcc version 5.4.0 ...
- C中的预编译宏定义
可以用宏判断是否为ARC环境 #if _has_feature(objc_arc) #else //MRC #endif C中的预编译宏定义 -- 作者: infobillows 来源:网络 在将一 ...
- C++头文件中预编译宏的目的
C++头文件中预编译宏的目的 eg: #ifndef _FACTORY_H_#define _FACTORY_H_......#endif //~_FACTORY_H_ 防止头文件被重复包含,导致变量 ...
- xcode 配置系统环境变量 Preporocessing 预编译宏的另一种写法, 系统的DEBUG 由来
在某些项目中看到一些环境变量类似宏的东西 比如叫ENVIRONMENT, 但发现还找不到具体这个宏是什么值, 那是因为他实在Preprocessing里配置了这个宏的值, 他能配置debug/rele ...
- Unity3D 多平台 预编译 宏定义
平台定义 UNITY_EDITOR 编辑器调用. UNITY_STANDALONE_OSX 专门为Mac OS(包括Universal,PPC和Intelarchitectures)平台的定义. UN ...
- C预编译宏
/* ============================================================================ Name : c_test001.c A ...
- C 预编译 宏 声明
- Visual Studio 2013 配置多个Release版本,并为每个版本配置预编译的宏
最近开发过程中,需要将同一份工程编译成两个版本,分别为ReleaseA和ReleaseB. ReleaseB比ReleaseA少了部分功能,所以希望在编译的时候,根据不同的版本编译工程中相应的代码. ...
随机推荐
- Column count of mysql.proc is wrong. Expected 20, found 16. Created with MySQL 50096, now running 50173.
IDEA链接mysql提示 Column count of mysql.proc is wrong. Expected 20, found 16. Created with MySQL 50096, ...
- JDBC batch批处理Statement executeBatch 具体解释
JDBC提供了数据库batch处理的能力,在数据大批量操作(新增.删除等)的情况下能够大幅度提升系统的性能.我曾经接触的一个项目,在没有採用batch处理时,删除5万条数据大概要半个小时左右,后来对系 ...
- Linux负载均衡软件LVS之二(安装篇)[转]
Linux负载均衡软件LVS之二(安装篇) 2011-04-26 16:01:47 标签:lvs安装配置 linux lvs 休闲 linux高可用 原创作品,允许转载,转载时请务必以超链接形式标明文 ...
- linux lvm的操作手册_pvcreate_vgcreate_lvcreate_相关
一. 前言 每个Linux使用者在安装Linux时都会遇到这样的困境:在为系统分区时,如何精确评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到当前某 个分区需要的容量,还要预见该分区以后可能需 ...
- Java 小型学生管理系统心得
这个学生管理系统相对来说比较简单,主要就是复习下java怎么连接数据库,然后你怎么来实现这个功能,我简单的说下思路吧. 首先你要构思好这个界面,他包括增删查改这些基本功能,然后你去分析这些功能都能怎么 ...
- MySQL数据库备份与恢复方法(转)
来源于:http://www.jb51.net/article/25686.htm 网站数据对我们对站长来说都是最宝贵的,我们平时应该养成良好的备份数据的习惯. 常有新手问我该怎么备份数据库, ...
- (转)XML CDATA是什么?
解析数据 XML 解析器通常情况下会处理XML文档中的所有文本. 当XML元素被解析的时候,XML元素内部的文本也会被解析: <message>This text is also pars ...
- Linux命令行文本处理工具
关键字搜索: grep 关键字 指定文件 -i 搜索时忽略大小写 -n 显示结果所在行 -v 显示不包含关键字的行 基于列的文本处理: cut -d: -f1 /etc/passwd -d ...
- Php 常用类
图表库下面的类库可以让你很简单就能创建复杂的图表和图片.当然,它们需要GD库的支持.pChart - 一个可以创建统计图的库.Libchart - 这也是一个简单的统计图库.JpGraph - 一个面 ...
- Quartz.net使用记录
1.引入dll文件: nuget控制台:安装quartz:Install-Package Quartz 安装log4net:Install-Package log4net,这里使用log4net记录一 ...