How do I check OS with a preprocessor directive?

前端 未结 16 1343
星月不相逢
星月不相逢 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:06

    You can use pre-processor directives as warning or error to check at compile time you don't need to run this program at all just simply compile it .

    #if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
        #error Windows_OS
    #elif defined(__linux__)
        #error Linux_OS
    #elif defined(__APPLE__) && defined(__MACH__)
        #error Mach_OS
    #elif defined(unix) || defined(__unix__) || defined(__unix)
        #error Unix_OS
    #else
        #error Unknown_OS
    #endif
    
    #include 
    int main(void)
    {
        return 0;
    }
    

提交回复
热议问题