views:

390

answers:

3

The question is quite clear I think. I'm trying to write a compiler detection header to be able to include in the application information on which compiler was used and which version.

This is part of the code I'm using:

/* GNU C Compiler Detection */
#elif defined __GNUC__
    #ifdef __MINGW32__
        #define COMPILER "MinGW GCC %d.%d.%d"
    #else
        #define COMPILER "GCC %d.%d.%d"
    #endif
    #define COMP_VERSION __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
#endif

Which could be used like this:

printf("  Compiled using " COMPILER "\n", COMP_VERSION);

Is there any way to detect LLVM and its version? And CLANG?

Thanks for your help.

+2  A: 

Snippet from InitPreprocessor.cpp:

  // Compiler version introspection macros.
  DefineBuiltinMacro(Buf, "__llvm__=1");   // LLVM Backend
  DefineBuiltinMacro(Buf, "__clang__=1");  // Clang Frontend

  // Currently claim to be compatible with GCC 4.2.1-5621.
  DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
  DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
  DefineBuiltinMacro(Buf, "__GNUC__=4");
  DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
  DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");

I didn't find any way to get the version of llvm and clang itself, though..

Christoph
i guess one could for now rely on the claimed GCC versioned supported for the features, and clang/llvm for extensions
Matt Joiner
A: 

For clang, you shouldn't test its version number, you should check for features you want with __has_feature and friends.

Chris Lattner
hm, this is a good point. can you provide a link to some official material regarding this?
Matt Joiner
+3  A: 

The '__llvm__' and '__clang__' defines are the official way to check for an LLVM compiler (llvm-gcc or clang) or clang, respectively.

'__has_feature' and '__has_builtin' are the recommended way of checking for optional compiler features when using clang, they are documented here: http://clang.llvm.org/docs/LanguageExtensions.html#feature%5Fcheck

Note that you can find a list of the builtin compiler defines for gcc, llvm-gcc, and clang using:

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

This preprocesses /dev/null as C, and uses -dM to spit out the definitions once preprocessing is done.

Daniel Dunbar