How do I check OS with a preprocessor directive?

前端 未结 16 1381
星月不相逢
星月不相逢 2020-11-22 13:53

I need my code to do different things based on the operating system on which it gets compiled. I\'m looking for something like this:

#ifdef OSisWindows
// do         


        
16条回答
  •  太阳男子
    2020-11-22 14:14

    On MinGW, the _WIN32 define check isn't working. Here's a solution:

    #if defined(_WIN32) || defined(__CYGWIN__)
        // Windows (x86 or x64)
        // ...
    #elif defined(__linux__)
        // Linux
        // ...
    #elif defined(__APPLE__) && defined(__MACH__)
        // Mac OS
        // ...
    #elif defined(unix) || defined(__unix__) || defined(__unix)
        // Unix like OS
        // ...
    #else
        #error Unknown environment!
    #endif
    

    For more information please look: https://sourceforge.net/p/predef/wiki/OperatingSystems/

提交回复
热议问题