C++ 在 CMake 中,如何测试编译器是否为 Clang?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10046114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:32:30  来源:igfitidea点击:

In CMake, how can I test if the compiler is Clang?

c++ccmakeclang

提问by leedm777

We have a set of cross-platform CMake build scripts, and we support building with Visual C++and GCC.

我们有一套跨平台的 CMake 构建脚本,我们支持使用Visual C++GCC构建。

We're trying out Clang, but I can't figure out how to test whether or not the compiler is Clang with our CMake script.

我们正在尝试Clang,但我不知道如何使用我们的 CMake 脚本测试编译器是否是 Clang 。

What should I test to see if the compiler is Clang or not? We're currently using MSVCand CMAKE_COMPILER_IS_GNU<LANG>to test for Visual C++ and GCC, respectively.

我应该测试什么来查看编译器是否是 Clang?我们目前分别使用MSVCCMAKE_COMPILER_IS_GNU<LANG>测试 Visual C++ 和 GCC。

回答by sakra

A reliable check is to use the CMAKE_<LANG>_COMPILER_IDvariables. E.g., to check the C++ compiler:

可靠的检查是使用CMAKE_<LANG>_COMPILER_ID变量。例如,要检查 C++ 编译器:

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 ccacheis used.

如果使用像ccache这样的编译器包装器,这些也能正常工作。

As of CMake 3.0.0 the CMAKE_<LANG>_COMPILER_IDvalue for Apple-provided Clang is now AppleClang. To test for both the Apple-provided Clang and the regular Clang use the following if condition:

从 CMake 3.0.0 开始CMAKE_<LANG>_COMPILER_ID,Apple 提供的 Clang 的值现在是AppleClang. 要测试 Apple 提供的 Clang 和常规 Clang,请使用以下 if 条件:

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  # using regular Clang or AppleClang
endif()

Also see the AppleClang policy description.

另请参阅AppleClang 政策说明

回答by arrowd

The OGRE 3D engine source code uses the following check:

OGRE 3D 引擎源代码使用以下检查

if (CMAKE_CXX_COMPILER MATCHES ".*clang")
    set(CMAKE_COMPILER_IS_CLANGXX 1)
endif ()

回答by Antonio

Just to avoid any mispelling problem, I am using this:

为了避免任何拼写错误的问题,我使用了这个:

if (CMAKE_CXX_COMPILER_ID MATCHES "[cC][lL][aA][nN][gG]") #Case insensitive match
    set(IS_CLANG_BUILD true)
else ()
    set(IS_CLANG_BUILD false)
endif ()

For making the regex case insensitive, I tried everything herewithout success (doesn't seem to be supported in CMake).

为了使正则表达式不区分大小写,我在这里尝试了所有方法都没有成功(CMake 似乎不支持)。

回答by Searene

This is a slightly more detailed answer for cmake newbies, modified from sakra's answer, just append the following lines toCMakeLists.txt:

这是针对 cmake 新手的稍微更详细的答案,根据 sakra 的答案进行了修改,只需将以下行附加到CMakeLists.txt

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  MESSAGE("Clang")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  MESSAGE("GNU")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
  MESSAGE("Intel")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  MESSAGE("MSVC")
endif()

Then run cmake .in the folder where CMakeLists.txt lies. Then you will see a bunch of outputs along with your answer.

然后cmake .在 CMakeLists.txt 所在的文件夹中运行。然后你会看到一堆输出以及你的答案。

...
-- Detecting CXX compile features
-- Detecting CXX compile features - done
GNU
-- Configuring done
-- Generating done
...