Macros for GCC/G++ to differentiate Linux and Mac OSX?
4 Answers
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
...

- 2,983
- 5
- 24
- 34
-
1@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 Apr 02 '10 at 17:22
-
11
-
-
5Note that there are different defines for C and C++; I had to add `-x c++` to the cpp command to get the values used for C++. – Emil Styrke Aug 13 '13 at 14:03
-
@Calmarius It's built into GCC. When it runs the preprocessor, it does so with these macros predefined. GCC exposes the -dM flag to list these macros. – themoondothshine Feb 05 '15 at 17:28
I'd be more inclined to test for feature availability than platform name. Try using autoconf.
Otherwise, this is a comprehensive list of platform defines.
Also check out this page for defines regarding compilers, libraries, architectures and devices.

- 618
- 1
- 8
- 18
-
1
-
+1 for the "predef" link. There are much more defines one level higher in the wiki. Let me improve your reply. – Mecki Mar 06 '12 at 21:49
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.

- 133,037
- 18
- 149
- 215
-
Yeah, I chose this way in my code. I just have Linux & Apple so no issues. Thanks. – Viet Apr 02 '10 at 10:33
-
2I find using compiler-defined macros like `__APPLE__` very useful because you can decouple the code from the build system, a lot of the time. Then it makes your like a lot easier when porting to esoteric platforms like Android or Chrome NaCl, which don't necessarily play nice with `./configure` and friends – Hans-Christoph Steiner Feb 02 '12 at 16:32
-
@Hans-Christoph: A good cross-compilation environment should help there. – Donal Fellows Feb 05 '12 at 13:58
-
The Android NDK is a good cross-compiler, and I do often use it to build projects with ./configure But for many projects its just so much easier to write a quick Android.mk, and then you get a nicely integrated build system for free. – Hans-Christoph Steiner Apr 18 '12 at 03:42
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.

- 208,748
- 37
- 389
- 560
-
1__APPLE__ covers Mac OS X and iOS, so its a better bet than __MACH__ IMHO. Its also more common from my experience – Hans-Christoph Steiner Feb 02 '12 at 16:31
-
-
I guess @Hans means `__APPLE__` ? But even then I'm not sure it's 100% reliable? – Paul R Aug 08 '13 at 15:00
-
Yes, thanks, I meant `__APPLE__` and `__MACH__`, looks like a wiki formatting error in my previous comment. – Hans-Christoph Steiner Aug 13 '13 at 19:05