可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
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)"
回答3:
For clang, you shouldn't test its version number, you should check for features you want with feature checking macros.
回答4:
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..
回答5:
Take a look at the Pre-defined Compiler Macros page, select Compilers->Clang. There is information on many other macros for standards, compilers, libraries, OS, architectures and more.
回答6:
I agree that the best choice is to use has feature macroses, not version macroses. Example with boost:
#include #if defined(BOOST_NO_CXX11_NOEXCEPT) #if defined(BOOST_MSVC) #define MY_NOEXCEPT throw() #else #define MY_NOEXCEPT #endif #else #define MY_NOEXCEPT noexcept #endif void my_noexcept_function() MY_NOEXCEPT; // it's example, use BOOST_NOEXCEPT (:
But anyway, if you need compiler version, you can use boost.predef:
#include #include int main() { #if (BOOST_COMP_CLANG) std::cout
Output examples:
Clang-30400000 Clang-50000000
回答7:
Note that if you're using llvm to hack on bytecode, and thus #includeing llvm include files, you can check the macros in llvm/Config/llvm-config.h. And concretely:
/* Major version of the LLVM API */ #define LLVM_VERSION_MAJOR 3 /* Minor version of the LLVM API */ #define LLVM_VERSION_MINOR 8 /* Patch version of the LLVM API */ #define LLVM_VERSION_PATCH 0 /* LLVM version string */ #define LLVM_VERSION_STRING "3.8.0"