第一种根据各个平台内置宏定义的宏:
Windows:
WIN32、_WIN32、_WIN32_、WIN64、_WIN64、_WIN64_
Android:
ANDROID、_ANDROID_
Linux:
__linux__
iOS/Mac:
__APPLE__、TARGET_OS_IPHONE、TARGET_IPHONE_SIMULATOR、TARGET_OS_MAC
#ifdef _WIN32
//define something for Windows (32-bit and 64-bit, this part is common)
#ifdef _WIN64
//define something for Windows (64-bit only)
#else
//define something for Windows (32-bit only)
#endif
#elif __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
# error "Unknown Apple platform"
#endif
#elif __ANDROID__
// android
#elif __linux__
// linux
#elif __unix__ // all unices not caught above
// Unix
#elif defined(_POSIX_VERSION)
// POSIX
#else
# error "Unknown compiler"
#endif
第二种根据各个平台常用的编译器的宏(编译器版本):
_MSC_VER:windows的vs编译器即cl.exe编译器,该宏扩展为int类型的数值;
__GNUC__:linux的gcc编译器,该宏扩展为int类型的数值;
__GNUG__:linux的g++编译器
__clang__:MacOS/IOS的clang编译器;
#if _MSC_VER >= 1910 //可以要求编译器版本
// code windows
#elif __GNUC__
// code linux
#elif __clang__
// code macos or ios
#endif