Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 489
mloskot Avatar asked Jan 30 '10 01:01

mloskot


People also ask

Is OS X written in Objective-C?

Objective-C was the standard programming language supported by Apple for developing macOS (which descended from NeXTSTEP) and iOS applications using their respective application programming interfaces (APIs), Cocoa and Cocoa Touch, until the introduction of Swift in 2014.

What is the full form of OS X?

OS X is version 10 of the Apple MacIntosh operating system . OS X was described by Apple as its first "complete revision" of the OS since the previous version is OS 9, with a focus on modularity so that future changes would be easier to incorporate.


1 Answers

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.

like image 167
Martin York Avatar answered Oct 06 '22 01:10

Martin York