How do I check OS with a preprocessor directive?

前端 未结 16 1346
星月不相逢
星月不相逢 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/

    0 讨论(0)
  • 2020-11-22 14:14

    Use #define OSsymbol and #ifdef OSsymbol where OSsymbol is a #define'able symbol identifying your target OS.

    Typically you would include a central header file defining the selected OS symbol and use OS-specific include and library directories to compile and build.

    You did not specify your development environment, but I'm pretty sure your compiler provides global defines for common platforms and OSes.

    See also http://en.wikibooks.org/wiki/C_Programming/Preprocessor

    0 讨论(0)
  • 2020-11-22 14:15

    Microsoft C/C++ compiler (MSVC) Predefined Macros can be found here

    I think you are looking for:

    • _WIN32 - Defined as 1 when the compilation target is 32-bit ARM, 64-bit ARM, x86, or x64. Otherwise, undefined
    • _WIN64 - Defined as 1 when the compilation target is 64-bit ARM or x64. Otherwise, undefined.

    gcc compiler PreDefined MAcros can be found here

    I think you are looking for:

    • __GNUC__
    • __GNUC_MINOR__
    • __GNUC_PATCHLEVEL__

    Do a google for your appropriate compilers pre-defined.

    0 讨论(0)
  • 2020-11-22 14:15

    Sorry for the external reference, but I think it is suited to your question:

    C/C++ tip: How to detect the operating system type using compiler predefined macros

    0 讨论(0)
  • 2020-11-22 14:21

    The Predefined Macros for OS site has a very complete list of checks. Here are a few of them, with links to where they're found:

    Windows

    _WIN32   Both 32 bit and 64 bit
    _WIN64   64 bit only

    Unix (Linux, *BSD, Mac OS X)

    See this related question on some of the pitfalls of using this check.

    unix
    __unix
    __unix__

    Mac OS X

    __APPLE__
    __MACH__

    Both are defined; checking for either should work.

    Linux

    __linux__
    linux Obsolete (not POSIX compliant)
    __linux Obsolete (not POSIX compliant)

    FreeBSD

    __FreeBSD__

    Android

    __ANDROID__

    0 讨论(0)
  • 2020-11-22 14:21

    show GCC defines on Windows:

    gcc -dM -E - <NUL:
    

    on Linux:

    gcc -dM -E - </dev/null
    

    Predefined macros in MinGW:

    WIN32 _WIN32 __WIN32 __WIN32__ __MINGW32__ WINNT __WINNT __WINNT__ _X86_ i386 __i386
    

    on UNIXes:

    unix __unix__ __unix
    
    0 讨论(0)
提交回复
热议问题