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

2019-01-31 17:42发布

问题:

While reading various C and C++ sources, I have encountered two macros __APPLE__ and __OSX__. I found plenty of use of __OSX__ in various codes, especially those originating from *BSD systems.

However, sometimes I find that testing __OSX__ only is not sufficient and I have to complete tests with __APPLE__ macro.

The Porting Command Line Unix Tools to Mac OS X guides specifies __APPLE__ and additionally __APPLE_CC__ but does not mention __OSX__.

The Porting from GCC guide says:

  • Use #ifdef __GNUC__ to wrap any GCC-specific code.
  • Use #ifdef __APPLE_CC__ to wrap any Mac OS X-specific code.

Again, no mention about __OSX__ macro.

What macro is predefined on Mac OS X platform and XCode development environment that should be used to distinguish OSX-specific code in C/C++ programs?

Where is the __OSX__ macro defined? Is it *BSD specific macro?

回答1:

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__

Presumabley the OS is a particular variant of OS X

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



回答2:

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__.



回答3:

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



回答4:

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.



回答5:

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