123

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 ?

2

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer omits the important piece of information, which is that __clang__ is indeed defined
96

Found the answer using strings + grep :

$ strings /usr/bin/clang | grep __ | grep -i clang
__clang__

Comments

48

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)

4 Comments

FYI, Windows equivalent of /dev/null is NUL:
@legalize Whoa! I didn't know that! That's pretty cool; where'd you find that?
It's been there since the DOS days, just most DOS/Windows users never needed it.
This is what I usually do in Linux as well. Typing /dev/null is more work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.