各个操作系统都有其对应的内置宏:

  1. Windows:WIN32_WIN32_WIN32_WIN64_WIN64_WIN64_
  2. Linux:_linux_
  3. Android:ANDROID_ANDROID_
  4. Mac/iPhone:_APPLE_TARGET_OS_IPHONETARGET_IPHONE_SIMULATORTARGET_OS_MAC

在C++代码中我们可以通过以下代码对不同操作系统的代码进行控制

#if defined(WIN32) || defined(_WIN32) || defined(_WIN32_) || defined(WIN64) || defined(_WIN64) || defined(_WIN64_) 
    // Windows平台代码
#elif defined(ANDROID) || defined(_ANDROID_)
    // Android平台代码
#elif defined(__linux__)
    // Linux平台代码
#elif defined(__APPLE__) || defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_MAC)
    // iOS、Mac平台代码
#else
    // 其他平台代码
#endif

或者

#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