What predefined macro can I use to detect clang?

CMacrosClangC Preprocessor

C Problem Overview


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 http://predef.sourceforge.net/ 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 ?

C Solutions


Solution 1 - C

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.

Solution 2 - C

Found the answer using strings + grep :

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

Solution 3 - C

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPierre BourdonView Question on Stackoverflow
Solution 1 - CChris SuterView Answer on Stackoverflow
Solution 2 - CPierre BourdonView Answer on Stackoverflow
Solution 3 - CBernd ElkemannView Answer on Stackoverflow