Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect LLVM and its version through #define directives?

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?

like image 362
Carla Álvarez Avatar asked Oct 24 '09 12:10

Carla Álvarez


People also ask

How do you check if I have LLVM?

The __llvm__ and __clang__ macros 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.

Where is LLVM installed?

The binaries for LLVM tools are placed in ~/llvm/build/bin . These tools are used to run the LLVM passes. To make it easy to run the tools, it is a good idea to add the path to the binaries to the bash PATH variable so that you can invoke the tools from any directory.

What version of C++ does LLVM use?

C++ Standard Versions Unless otherwise documented, LLVM subprojects are written using standard C++17 code and avoid unnecessary vendor-specific extensions.


2 Answers

The __llvm__ and __clang__ macros 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.

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

echo | clang -dM -E - 

This preprocesses an empty string and spits out all macros defined by the compiler.

like image 96
Daniel Dunbar Avatar answered Sep 18 '22 22:09

Daniel Dunbar


I cannot find an answer here, only links to answers, so for completeness, here is the answer:

__clang__             // set to 1 if compiler is clang __clang_major__       // integer: major marketing version number of clang __clang_minor__       // integer: minor marketing version number of clang __clang_patchlevel__  // integer: marketing patch level of clang __clang_version__     // string: full version number 

I get currently:

__clang__=1 __clang_major__=3 __clang_minor__=2 __clang_patchlevel__=0 __clang_version__="3.2 (tags/RELEASE_32/final)" 
like image 31
Walter Avatar answered Sep 18 '22 22:09

Walter