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

后端 未结 6 1703
悲哀的现实
悲哀的现实 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 
    #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.

提交回复
热议问题