各个平台和编译器预定义的宏

各个平台和编译器预定义的宏

Zhiyu Lv4

在跨平台编程中,通过宏来区分不同的平台是一种较为常见的方法。

1 不同平台

1.1 Windows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifdef _WIN32
//is defined for windows (32-bit and 64-bit)
#ifdef _WIN64
//windows 64bits
#else
//define something for windows (32-bit only)
#endif

//GUI App
#ifdef _WINDOWS

//CUI App
#ifdef _CONSOLE

//Windows Versions
#ifdef WINVER // or #ifdef _WIN32_WINNT
#if (WINVER >= 0x030a) //Windows 3.1 and above
#if (WINVER >= 0x0400) //Windows 95/NT4.0 and above
#if (WINVER >= 0x0410) //Windows 98 and above
#if (WINVER >= 0x0500) //Windows Me/2000 and above
#if (WINVER >= 0x0501) //Windows XP and above
#if (WINVER >= 0x0600) //Windows Vista and above
#if (WINVER >= 0x0601) //Windows 7 and above


//DOS
#ifdef __DOS__
#ifdef __MSDOS__

//Cygwin
#ifdef __CYGWIN__

1.2 Unix

1
2
3
4
5
6
7
8
9
10
11
12
13
//UNIX
#ifdef __unix
#ifdef __unix__

//Linux
#ifdef __linux
#ifdef __linux__

//FreeBSD
#ifdef __FreeBSD__

//NetBSD
#ifdef __NetBSD__

1.3 MacOS & iOS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Universal
#ifdef __APPLE__
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
#elif TARGET_OS_IPHONE
// iOS device
#elif TARGET_OS_MAC
// Other kinds of Mac OS
#else
//Unknown Apple platform
#endif

// Mac OS 9
#ifdef Macintosh

//Mac OS X
#ifdef __APPLE__&&__MACH__

1.4 Android

1
2
3
4
5
6
#ifdef __ANDROID__
#if __ANDROID_API__ >= 1 //Android Version >= 1.0
#if __ANDROID_API__ >= 5 //Android Version >= 2.0
#if __ANDROID_API__ >= 11 //Android Version >= 3.0
#if __ANDROID_API__ >= 1 //Android Version >= 1.0
#if __ANDROID_API__ >= 1 //Android Version >= 1.0

2 不同编译器

2.1 GCC

1
2
#ifdef __GNUC__
#if __GNUC__ >= 3 //GCC-3.0 and above

2.2 Visual C++

1
2
3
4
5
6
7
8
9
10
11
12
#ifdef _MSC_VER (非VC编译器很多地方也有定义)
#if _MSC_VER >= 1000 //MSVC++4.0 and above
#if _MSC_VER >= 1100 //MSVC++5.0 and above
#if _MSC_VER >= 1200 //MSVC++6.0 and above
#if _MSC_VER >= 1300 //MSVC++7.0 and above
#if _MSC_VER >= 1400 //MSVC++8.0 and above
#if _MSC_VER >= 1500 //MSVC++9.0 (Visual Studio 2008) and above
#if _MSC_VER >= 1600 //MSVC++10.0 (Visual Studio 2010) and above
#if _MSC_VER >= 1700 //MSVC++11.0 (Visual Studio 2012) and above
#if _MSC_VER >= 1800 //MSVC++12.0 (Visual Studio 2013) and above
#if _MSC_VER >= 1900 //MSVC++14.0 (Visual Studio 2015) and above
#if _MSC_VER >= 1910 //MSVC++14.1 (Visual Studio 2017) and above

2.3 Borland C++

1
#ifdef __BORLANDC__

2.4 CLANG

1
#ifdef 

3 其它

todo

  • Title: 各个平台和编译器预定义的宏
  • Author: Zhiyu
  • Created at : 2019-11-14 22:44:37
  • Updated at : 2026-02-18 00:46:03
  • Link: https://qgrain.github.io/2019/11/14/各个平台和编译器预定义的宏/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments