Skip to content

Latest commit

 

History

History
39 lines (34 loc) · 1.06 KB

137 - 如何通过宏来判断当前的系统类型.md

File metadata and controls

39 lines (34 loc) · 1.06 KB

https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor

问题

如何通过宏来判断当前的系统类型?

回答

在这里你可以找到所有平台的预定义宏:https://sourceforge.net/p/predef/wiki/OperatingSystems/ ,下面是一个代码示例,

#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