search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C/C++ 判斷OS(windows/linux) ( C/C++ compiling on Windows and Linux: ifdef switch [duplicate]) – ja

C/C++ 判斷OS(windows/linux) ( C/C++ compiling on Windows and Linux: ifdef switch [duplicate])


資料來源: https://stackoverflow.com/questions/6649936/c-compiling-on-windows-and-linux-ifdef-switch

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


範例測試編譯器:

https://www.tutorialspoint.com/compile_c_online.php
CodeBlocks 12.11


語法:[判斷作業系統/平台/os]

#ifdef __linux__ 
    //linux code goes here
#elif _WIN32
    // windows code goes here
#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 
    #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

範例:

#include 
#include 

/*
//https://stackoverflow.com/questions/6649936/c-compiling-on-windows-and-linux-ifdef-switch

#ifdef __linux__
    //linux code goes here
#elif _WIN32
    // windows code goes here
#else

#endif
*/

int main()
{
#ifdef __linux__
    //linux code goes here
    printf("Hello world LINUX!\n");
#elif _WIN32
    // windows code goes here
    printf("Hello world WINDOWS!\n");
#else

#endif

    return 0;
}



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦