c++跨平台编译宏区分的两种办法

第一种根据各个平台内置宏定义的宏:

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

参考:

https://www.cnblogs.com/fortunely/p/18021414

linux, windows, mac, ios等平台GCC预编译宏判断 - 简书

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值