Which macro to wrap Mac OS X specific code in C/C++

后端 未结 6 1747
悲哀的现实
悲哀的现实 2020-12-13 03:32

While reading various C and C++ sources, I have encountered two macros __APPLE__ and __OSX__. I found plenty of use of __OSX__ in vari

相关标签:
6条回答
  • 2020-12-13 04:11

    See http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system#OSXiOSandDarwin

    #ifdef __APPLE__
    #include <TargetConditionals.h>
    #if TARGET_OS_MAC
       ...
    #endif /* TARGET_OS_MAC */
    #endif /* __APPLE__ */
    

    Note that __OSX__ does NOT exist, at least as of Xcode 9.

    Also note that it is #if TARGET_OS_MAC not #ifdef. It is always defined, but is 0 when not macOS.

    0 讨论(0)
  • 2020-12-13 04:19

    It all depends.

    Each macro specifies something different in meaning.
    See: https://developer.apple.com/library/mac/documentation/Porting/Conceptual/PortingUnix/compiling/compiling.html#//apple_ref/doc/uid/TP40002850-SW13

    __APPLE__

    This macro is defined in any Apple computer.

    __APPLE_CC__

    This macro is set to an integer that represents the version number of the compiler. This lets you distinguish, for example, between compilers based on the same version of GCC, but with different bug fixes or features. Larger values denote later compilers.

    __OSX__

    Presumably the OS is a particular variant of OS X

    So given the above definitions I would use __APPLE__ to distinguish apple specific code.

    0 讨论(0)
  • 2020-12-13 04:20

    For anyone coming across this question >= 2019, I found there's a header "Availability.h".

    In that header, are #defines like:

    #define __MAC_10_0            1000
    #define __MAC_10_1            1010
    #define __MAC_10_2            1020
    #define __MAC_10_3            1030
    #define __MAC_10_4            1040
    #define __MAC_10_5            1050
    #define __MAC_10_6            1060
    #define __MAC_10_7            1070
    #define __MAC_10_8            1080
    #define __MAC_10_9            1090
    #define __MAC_10_10         101000
    #define __MAC_10_10_2       101002
    #define __MAC_10_10_3       101003
    #define __MAC_10_11         101100
    #define __MAC_10_11_2       101102
    

    So you CAN tell if you're compiling on a particular MacOS platform.

    0 讨论(0)
  • 2020-12-13 04:22

    I normally use __MACH__ for this. It's been defined since the earliest version of OS X (and even before, presumably).

    0 讨论(0)
  • 2020-12-13 04:29

    Here is a nice list of macros for operating systems.

    There's little info on __OSX__ on the web. You'll be safe with __APPLE__.

    0 讨论(0)
  • 2020-12-13 04:37

    Use

    #if defined(__APPLE__) && defined(__MACH__)
    

    to distinguish Apple MacOS (not iOS).

    Regarding the "where does OSX come from":

    Some on-line lists of compiler macros (like this one) list __MACOSX__. Some forum comments (like these) claim __OSX__ exists. These are incorrect. There are no such macros predefined by OSX compilers, but they may be defined by specific project Makefiles and platform-detector scripts like GNU autoconf.

    Source: http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

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