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

最佳答案

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

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

我刚刚用

运行了一个成功的测试
  • LLVM-3.9.0-r273898-win32.exe来自 http://llvm.org/builds/
  • cmake-3.6.0-rc4-win64-x64.msi来自 https://cmake.org/download/
  • Microsoft VS2015 社区版版本 14.0.23107.0

在全局 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 项目的属性页面中交叉检查可用的“平台工具集”:

引用文献

  • What is the -D define to tell Cmake where to find nmake?
  • Linker for Clang?
  • Switching between GCC and Clang/LLVM using CMake

https://stackoverflow.com/questions/38171878/

相关文章:

java - 如何使用 Jenkins 参数化构建?

gradle - 如何更改 gradle 插件存储库?

command-line - 如何从命令行构建 IntelliJ 项目?

plugins - 未找到自定义 Gradle 插件 ID

ios - 如何在 Xcode 4 中自动增加捆绑版本?

c++ - CMake ExternalProject_Add() 和 FindPackage()

xcode - 了解 Xcode 的 Copy Headers 阶段

java - 错误 :Could not initialize class com. android

android - 多项目结构的 Gradle 构建速度非常慢

visual-studio-2010 - Visual Studio 2010 中的项目构建顺序?