如何告诉 CMake 在 Windows 上使用 Clang?

新手上路,请多包涵

我有一个使用 CMake 构建的 C++ 项目。我通常在 OSX 上构建,但现在我正在尝试让 Windows 版本也能正常工作。出于兼容性原因,我想在 Windows 上使用 Clang。

我从 LLVM 安装了预编译的 Clang 3.8 二进制文件:

 C:\Program Files\LLVM\bin\clang.exe
C:\Program Files\LLVM\bin\clang++.exe

它也安装在我的 PATH 上:

 >clang++
clang++.exe: error: no input files

我有两个问题:

  1. 当我调用 --- 时,如何告诉 CMake 使用 clang++ cmake --build
  2. 在构建 CMake 配置的编译器之前如何检查?

原文由 sdgfsdh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.4k
2 个回答

除了 Clang 编译器本身之外,您还需要一个用于 Windows 的构建/链接环境。

最新的 CMake 3.6 构建确实在 Windows 上集成了几个受支持的 Clang 构建环境(例如 Visual Studio、Cygwin;请参阅 发行说明)。

我刚刚进行了成功的测试

所有安装到它们的标准路径,它们的 bin 全局 PATH 环境中的目录。

您需要知道的部分是使用 CMake -T"LLVM-vs2014" 命令行选项设置正确的工具集。在配置过程中,CMake 会让您知道它找到/采用了哪个编译器。

CMakeLists.txt

 cmake_minimum_required(VERSION 3.6)

project(HelloWorld)

file(
    WRITE main.cpp
        "#include <iostream>\n"
        "int main() { std::cout << \"Hello World!\" << std::endl; return 0; }"
)
add_executable(${PROJECT_NAME} main.cpp)

Windows 控制台

...> mkdir VS2015
...> cd VS2015
...\VS2015> cmake -G"Visual Studio 14 2015" -T"LLVM-vs2014" ..
-- The C compiler identification is Clang 3.9.0
-- The CXX compiler identification is Clang 3.9.0
-- Check for working C compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: .../VS2015
...\VS2015> cmake --build .
Microsoft (R)-Buildmodul, Version 14.0.23107.0
[...]
...\VS2015> Debug\HelloWorld.exe
Hello World!

安装提示

请注意,我在设置过程中已将 LLVM 添加到我的搜索路径中:

使用添加到 PATH 的 LLVM 安装

您可以在任何 VS 项目的属性页面中交叉检查可用的“平台工具集”:

VS 项目属性 - 平台工具集

参考

原文由 Florian 发布,翻译遵循 CC BY-SA 3.0 许可协议

作为 Visual Studio 2019 的更新,您可以通过 Visual Studio 安装程序 安装 clang-cl 工具链并使用它来生成 .sln 文件:

 > mkdir build && cd build
> cmake .. -G "Visual Studio 16 2019" -T ClangCL -A x64

当然你现在也可以直接打开包含 CMakeLists.txt 文件的文件夹,VS 会提供不错的支持,允许你选择编译器工具链,但是它不会让你使用图形调试器,这可能是对你很重要。

原文由 trojanfoe 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题