如何测试编译器是否为

如何测试编译器是否为

本文介绍了在 CMake 中,如何测试编译器是否为 Clang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我们正在尝试 Clang,但我不知道如何测试编译器是带有我们 CMake 脚本的 Clang.

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?我们目前正在使用 MSVCCMAKE_COMPILER_IS_GNU 分别测试 Visual C++ 和 GCC.

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

推荐答案

一个可靠的检查是使用 CMAKE__COMPILER_ID 变量.例如,检查 C++ 编译器:

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()

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

These also work correctly if a compiler wrapper like ccache is used.

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

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()

另请参阅 AppleClang 政策说明.

CMake 3.15 增加了对 clang-cl 和常规的 clang 前端.您可以通过检查变量 CMAKE_CXX_COMPILER_FRONTEND_VARIANT 来确定前端变体:

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()

这篇关于在 CMake 中,如何测试编译器是否为 Clang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:53