views:

661

answers:

4

Macros for GCC/G++ to differentiate Linux and Mac OSX?

+1  A: 

I'd be more inclined to test for feature availability than platform name. Try using autoconf. Otherwise, this is a comprehensive list.

Graham Lee
Thanks but I avoid autoconf. I just want to use simple macros.
Viet
+2  A: 

I use __MACH__ to test for Mac OS X - it's not 100% unique to Mac OS X (there may still be some old NeXT boxes out there !) but it's good enough for telling the difference between Mac and Linux.

Paul R
+2  A: 

Detect OSX with the __APPLE__ macro if you must. It's better to use configure to detect features if you can, but not everything works well that way.

Donal Fellows
Viet
+3  A: 

The next time you want to check out pre-defined macros supported by GCC on a platform, run the preprocessor with the flag -dM. It'll list out all the predefined macros available on the system. For example:

$ touch dummy.hxx
$ cpp -dM ./dummy.hxx
#define __DBL_MIN_EXP__ (-1021)
#define __FLT_MIN__ 1.17549435e-38F
#define __CHAR_BIT__ 8
#define __WCHAR_MAX__ 2147483647
#define __DBL_DENORM_MIN__ 4.9406564584124654e-324
#define __FLT_EVAL_METHOD__ 0
#define __DBL_MIN_10_EXP__ (-307)
#define __FINITE_MATH_ONLY__ 0
#define __SHRT_MAX__ 32767
#define __LDBL_MAX__ 1.18973149535723176502e+4932L
#define __UINTMAX_TYPE__ long unsigned int
#define __linux 1
#define __unix 1
#define __linux__ 1
...
themoondothshine
+1 It works like a charm! How I cannot know this! Thanks!
Viet
@Viet: You're welcome... actually I ran into a similar conundrum as yours recently. I wanted to know if there was a pre-defined macro indicating the byte order of the machine. I hit upon this in some obscure documentation after hours and hours of searching!
themoondothshine