Compiler Detection

On both PowerPC and Intel, Apple's compiler is [GCC]. In the past, the compiler of choice for the Mac OS has been Metrowerks CodeWarrior, but it has become less and less supported as time goes on and is no longer suitable for moden Mac development.

Historically, Mac OS X has used many different versions of GCC:

Mac OS X 10.0 - 10.1 (ProjectBuilder)gcc 2.95.2
Mac OS X 10.2 (ProjectBuilder)gcc 3.1
Mac OS X 10.3 - 10.4 (ProjectBuilder, XCode)gcc 3.3
Mac OS X 10.4 PPC (Xcode)gcc 4.0.1 ppc
Mac OS X 10.4 Intel (Xcode)gcc 4.0.1 i386

None of these are standard versions of GCC; instead, Apple maintains a branch off the mainline of GCC development. This results in a significant delay between updates to GCC by the GCC team and versions of GCC suitable for use by Mac OS X. However, it is necessary in order for Apple to provide a number of features critical to Mac OS X. Some examples are:

When building code with other compilers, or cross-platform code, you may wish to conditionalise code based on the current compiler. You can always detect GCC itself with the __GNUC__ macro, and Apple's GCC with the __APPLE__ macro. The simplest way to detect more specific diferences is to use the macros provided by TargetConditionals.h. In Mac OS X 10.3.x and 10.4.x, it is located in /usr/include/, which is normally not visible in the Finder unless you use the "Go To Folder..." command in the Go menu. It is included automatically by almost every framework provided by Mac OS X; if you already include CoreFoundation or Carbon, you have TargetConditionals.h. This header includes macros for CPU detection:

TARGET_CPU_PPCCompiler is generating PowerPC instructions
TARGET_CPU_X86Compiler is generating x86 instructions

OS detection:

TARGET_OS_MACGenerated code will run under Mac OS
TARGET_OS_WIN32Generated code will run under 32-bit Windows
TARGET_OS_UNIXGenerated code will run under some non-Mac Unix (e.g., Linux)

endian-ness:

TARGET_RT_LITTLE_ENDIANGenerated code uses little endian format
TARGET_RT_BIG_ENDIANGenerated code uses big endian format

and the executable format:

TARGET_RT_MAC_MACHOGenerated code uses the Mach-O/dyld runtime
TARGET_RT_MAC_CFMGenerated code uses the PEF/CFM runtime (This will never be 1 if you are building with Xcode/GCC)

These macros are always defined as long as TargetConditionals.h is included, so they should be tested with #if rather than #ifdef.