What predefined macro can I use to detect clang?
Asked Answered
T

3

122

I'm trying to detect the compiler used to compile my source code. I can easily find predefined macros to check for MSVC or GCC (see https://github.com/cpredef/predef for example), but I cannot find any macro to check for clang.

Does someone know if clang defines a macro like __CLANG__ in order to know what is currently compiling my code ?

Trauner answered 17/4, 2010 at 13:12 Comment(2)
predef.sourceforge.net/precomp.html#sec4Waterless
Updated list of compiler macrosSaintmihiel
T
93

Found the answer using strings + grep :

$ strings /usr/bin/clang | grep __ | grep -i clang
__clang__
Trauner answered 17/4, 2010 at 13:23 Comment(0)
A
135

To get a list of all the predefined macros that the compiler uses, use this:

clang -dM -E -x c /dev/null

You can do the same for gcc.

Am answered 29/4, 2011 at 4:0 Comment(0)
T
93

Found the answer using strings + grep :

$ strings /usr/bin/clang | grep __ | grep -i clang
__clang__
Trauner answered 17/4, 2010 at 13:23 Comment(0)
S
44

This question has been answered for years but let me add (for future reference) how it is done in Windows:

echo | clang -dM -E -

same as for GCC:

echo | gcc -dM -E -

Please note: The last dash - is actually important! (Otherwise you get error: no input files for both compilers)

Spadix answered 27/7, 2012 at 10:37 Comment(4)
FYI, Windows equivalent of /dev/null is NUL:Reverent
@Reverent Whoa! I didn't know that! That's pretty cool; where'd you find that?Rightly
It's been there since the DOS days, just most DOS/Windows users never needed it.Reverent
This is what I usually do in Linux as well. Typing /dev/null is more work.Hon

© 2022 - 2024 — McMap. All rights reserved.