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

sdg*_*sdh 24 c++ windows build cmake 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
Run Code Online (Sandbox Code Playgroud)

它也安装在我的PATH上:

>clang++
clang++.exe: error: no input files
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

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

Flo*_*ian 35

除了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)
Run Code Online (Sandbox Code Playgroud)

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!
Run Code Online (Sandbox Code Playgroud)

安装提示

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

使用

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

VS项目属性 - 平台工具集

参考


tro*_*foe 26

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

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

Run Code Online (Sandbox Code Playgroud)

或者

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

Run Code Online (Sandbox Code Playgroud)

然而,更好的工作流程是简单地打开包含CMakeLists.txt,它会将其识别为 CMake 项目并提供直接设置编译器等的功能。

我发现这种方法的唯一缺点是 Visual Studio 图形调试器工作流程无法使用它,如果您使用 DirectX 等进行开发,这会影响您。

  • 在 VS 2022 中,使用“ClangCL”工具集时,我收到 CMake 错误:“CXX 编译器标识未知 CMake Lists.txt:1 (项目) 处的 CMake 错误:找不到 CMAKE_C_COMPILER。”。知道可能出什么问题吗?我已经通过 VS 安装程序安装了 Clang 工具。 (2认同)

Ami*_*adi 13

请遵循以下说明:

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

choco install ninja -y
choco install cmake -y
choco install llvm -y
Run Code Online (Sandbox Code Playgroud)

重置您的 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
Run Code Online (Sandbox Code Playgroud)

从图形用户界面

转到您的项目并运行:

cmake-gui .
Run Code Online (Sandbox Code Playgroud)

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

选择“Ninja Multi-Config”并指定本机编译器:

忍者

给出编译器的路径: 编译器

最后,运行

cmake --build ./build --config Release
Run Code Online (Sandbox Code Playgroud)

使用 Visual Studio

来自 PowerShell

cmake -S ./ -B ./build
cmake --build ./build --config Release
Run Code Online (Sandbox Code Playgroud)

从图形用户界面

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

git clone https://github.com/zufuliu/llvm-utils.git
cd llvm-utils/VS2017
.\install.bat
Run Code Online (Sandbox Code Playgroud)

转到您的项目并运行:

cmake-gui .
Run Code Online (Sandbox Code Playgroud)

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

选择 Visual Studio 2019 和第二个选项(指定本机编译器)。适用 LLVM_v142于 Visual Studio 2019 及更高版本。请参阅此处了解其他人的 旧版本。视觉工作室

给出编译器的路径: 编译器

最后,运行

cmake --build ./build --config Release
Run Code Online (Sandbox Code Playgroud)