【问题标题】:How do I tell CMake to use Clang on Windows?如何告诉 CMake 在 Windows 上使用 Clang?
【发布时间】:2016-11-05 10:16:26
【问题描述】:

我有一个使用 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 --build 时,如何告诉 CMake 使用 clang++
  2. 在构建 CMake 配置的编译器之前如何检查?

【问题讨论】:

  • toolchain.txt 文件,在文档中查找这个。
  • 在 ubuntu 上,当我通过 Make 调用 g++ 编译器时,我使用 CC='g++ -m64'(Make 中有 $(CC))我使用 CC="clang++ -m64' 来调用 clang++ . (我不使用 CMake 也不使用 Windows)
  • 使用-DCMAKE_CXX_COMPILER=C:/path/to/clang++有什么问题?
  • @usr1234567 它依赖于平台,即不能使用像cmake .. && cmake --build . 这样的单一便携式配方,但除了windos之外必须使用。

标签: c++ windows build cmake clang


【解决方案1】:

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

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

我刚刚用

运行了一个成功的测试

在全局PATH 环境中使用bin 目录安装到它们的标准路径。

您需要了解的部分是使用 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 添加到我的搜索路径中:

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

参考文献

【讨论】:

  • 谢谢!我缺少的一点是-T"LLVM-vs2014"
  • 非常混乱。 CMake 应该考虑将 -G 更改为 MSBuild,因为这是它输出的内容,以及设置实际编译器工具链的子选项
  • 很奇怪。使用 Microsoft Visual Studio 2015、CMake 3.7.1 和 LLVM/Clang 3.9.1 执行此操作时,CMake 仍会在我的机器上选择默认的 cl.exeThe CXX compiler identification is MSVC 19.0.24215.1
  • @FlorianWolters 不同之处可能是我的搜索路径中有clang。如果您直接从命令行调用clang,它是否找到编译器并显示clang.exe: error: no input files
  • (几乎)可移植的解决方案,如果不运行 LLVM 安装程序:用字符串 &lt;LLVMInstallDir&gt;$(LLVM_ROOT)&lt;/LLVMInstallDir&gt; 替换 dir tools\msbuild 下所有 .props 文件中的字符串 &lt;LLVMInstallDir&gt;$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\LLVM\LLVM)&lt;/LLVMInstallDir&gt; 并确保 env 变量LLVM_ROOT 指向“便携式”安装的目录,例如C:\llvm-3.9.1-x86问题:“安装”MSBuild 的 LLVM 工具集需要管理员权限。如果有人知道如何使用未存储在默认目录中的 MSBuild 工具集,请告诉我。
【解决方案2】:

作为 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会提供不错的支持,允许你选择编译器工具链,但是它不会让你使用图形调试器,这可能很重要给你。

【讨论】:

  • 感谢您的命令,只是想通知您现在也可以使用调试器了。
  • @AlexG.G. Visual Studio 图形调试器?
  • Nvm 我已经理解了要调试的 ui,但您的意思是调试图形的工具对吗?
【解决方案3】:

按照以下说明进行操作:

如果没有 choco,请安装:https://chocolatey.org/install

choco install ninja -y
choco install cmake -y
choco install llvm -y

重置您的 shell,以便正确设置环境变量(您可以检查是否将每个 bin 文件夹添加到您的路径中)。

使用忍者

来自 PowerShell

$env:CC="C:\Program Files\LLVM\bin\clang.exe"
$env:CXX="C:\Program Files\LLVM\bin\clang++.exe"
cmake -S ./ -B ./build -G "Ninja-Multi-Config"
cmake --build ./build --config Release

从图形用户界面

转到您的项目并运行:

cmake-gui .

从上方菜单中选择Tools/Configure 并按照以下设置:

选择“Ninja Multi-Config”并指定原生编译器:

给出编译器的路径:

最后,运行

cmake --build ./build --config Release

使用 Visual Studio

使用图形用户界面

在某个文件夹中安装 llvm-utils:

git clone https://github.com/zufuliu/llvm-utils.git
cd llvm-utils/VS2017
.\install.bat

转到您的项目并运行:

cmake-gui .

从上方菜单中选择Tools/Configure 并遵循以下设置:

选择 Visual Studio 2019 和第二个选项(指定本机编译器)。放 LLVM_v142 适用于 Visual Studio 2019 及更高版本。其他人见here for older versions

给出编译器的路径:

最后,运行

cmake --build ./build --config Release

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2019-02-20
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多