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

标签 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

它也安装在我的 PATH 上:

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

我有两个问题:

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

最佳答案

除了 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 添加到我的搜索路径中:

LLVM Installation with Add to PATH

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

VS Project Properties - Platform Toolsets

引用文献

关于c++ - 如何告诉 CMake 在 Windows 上使用 Clang?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38171878/

相关文章:

c++ - 适用于 Windows 的 Eclipse - 使用 libPNG

windows - 使用 forfiles 删除文件夹及其内容

windows - 使用批处理文件从不同时区获取本地时间

build - Docker Hub 构建分支正则表达式匹配而不是 master

c# - Xamarin:构建时出错 -> 无效的文件路径 'obj\Debug\90\res\views\layouttest.xml'

c++ - 排序和扫描对报告中的性能问题

c++ - 访问冲突读取位置0xfeeeefef2 多线程编程c++ windows

java - 如何在缺少参数的情况下使 Maven 构建失败?

c++ - 如何在 C++ 中将 CHAR 变量复制到 WCHAR 变量

c++ - 动态分配的数组未释放