118

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 ?

AJM
  • 1,317
  • 2
  • 15
  • 30
Pierre Bourdon
  • 10,521
  • 4
  • 33
  • 27

3 Answers3

133

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.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
Chris Suter
  • 2,897
  • 2
  • 19
  • 10
89

Found the answer using strings + grep :

$ strings /usr/bin/clang | grep __ | grep -i clang
__clang__
Pierre Bourdon
  • 10,521
  • 4
  • 33
  • 27
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)

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66