C/C++ 跨平台时预处理判断平台环境

在编写跨平台的 C/C++ 时,需要判断平台的操作系统类型。通过预编译宏,区分各平台。

参考 How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor? [duplicate]

注:

  • WIN32在 win32 和 win64 下都会被定义,所以先判 WIN64
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
   //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 __linux__
    // linux
#elif __unix__ // all unices not caught above
    // Unix
#elif defined(_POSIX_VERSION)
    // POSIX
#else
#   error "Unknown compiler"
#endif

原文链接: https://www.cnblogs.com/Forgenvueory/p/12757271.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C/C++ 跨平台时预处理判断平台环境

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/196372

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午7:14
下一篇 2023年2月12日 下午7:14

相关推荐