How do I check OS with a preprocessor directive?

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

提交回复
热议问题