tags:

views:

197

answers:

3

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  A: 

It all depneds.

Each macro specifes somthing different in meaning.
See: http://developer.apple.com/mac/library/technotes/tn2002/tn2071.html

__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 distingush apple specific code.

Martin York
Yes, I understand the difference of `__APPLE__` and `__APPLE_CC__` macros - I linked from my questions to the very same guide. BTW, you included `__OSX__` macro within the guide citation and one may understand it is explanation from the linked guide, but it is your comment. I got a bit confused :-)
mloskot
+1  A: 

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

Kornel Kisielewicz
+1 to you and Martin York. Thanks! However, I am still very curious about where `__OSX__` comes from, so I'll wait a bit with marking my question as answered.
mloskot
+1  A: 

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

Paul R
`__MACH__` is used for GNU/Hurd too because the latter currently uses a Mach kernel.
KennyTM
@KennyTM - thanks for that - I didn't know here were any other systems out there that use `__MACH__`. For my purposes it's good enough, though, as I typically only care about Mac OS X v Linux v Windows.
Paul R
Paul, thanks for the tip, it's useful but it doesn't really answer my original question. I'd like to precisely know where the `__OSX__` comes from.
mloskot
OK - sorry - I thought you just wanted suggestions as to what macro to use for OS X-specific code.
Paul R
@Paul, Yes, I'm asking for such macro, but macro which is uniquely specified on Mac OS X and XCode. AFAIU, `__MACH__` is not unique for OSX. Thanks anyway.
mloskot