A reliable check is to use the CMAKE_<LANG>_COMPILER_ID
variables. E.g., to check the C++ compiler:
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# using Clang
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# using GCC
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
# using Intel C++
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# using Visual Studio C++
endif()
These also work correctly if a compiler wrapper like ccache is used.
As of CMake 3.0.0 the CMAKE_<LANG>_COMPILER_ID
value for Apple-provided Clang is now AppleClang
. To test for both the Apple-provided Clang and the regular Clang use the following if condition:
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# using regular Clang or AppleClang
endif()
Also see the AppleClang policy description.
CMake 3.15 has added support for both the clang-cl and the regular clang front end. You can determine the front end variant by inspecting the variable CMAKE_CXX_COMPILER_FRONTEND_VARIANT
:
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
# using clang with clang-cl front end
elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
# using clang with regular front end
endif()
endif()
__GNUC__
and_MSC_VER
, but it can't consume the same programs as either compiler. Detecting LLVM Clang and Apple Clang is crucial to ensuring the code compiles and executes as expected. I'm so tired of dealing with Clang's BS we just break the compile on Windows. We've adopted the policy of let users complain to LLVM so the Clang devs change their behavior. Also see How to tell Clang to stop pretending to be other compilers? – jww