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

How do I tell CMake to use Clang on Windows?(如何告诉 CMake 在 Windows 上使用 Clang?)

本文介绍了如何告诉 CMake 在 Windows 上使用 Clang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a C++ project that builds using CMake. I usually build on OSX but now I am trying to get a Windows version working too. I would like to use Clang on Windows for compatibility reasons.

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

I installed the pre-compiled Clang 3.8 binary from LLVM:

C:Program FilesLLVMinclang.exe
C:Program FilesLLVMinclang++.exe

它也安装在我的 PATH 上:

It is also installed on my PATH:

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

我有两个问题:

  1. 当我调用 cmake --build 时,如何告诉 CMake 使用 clang++?
  2. 如何在构建 CMake 配置的编译器之前检查?
  1. How do I tell CMake to use clang++ when I call cmake --build?
  2. How can I check before building which compiler CMake is configured with?

推荐答案

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

You also need - in addition to the Clang compilers itself - an build/link environment for Windows.

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

The latest CMake 3.6 builds do have several integrated supported Clang build environments on Windows (e.g. Visual Studio, Cygwin; see 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 目录.

All installed to their standard paths with their bin directories in the global PATH environment.

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

The part you need to know is setting the right toolset with the CMake -T"LLVM-vs2014" command line option. During the configuration process CMake will let you know which compiler it has found/taken.

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)

project(HelloWorld)

file(
    WRITE main.cpp 
        "#include <iostream>
"
        "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> DebugHelloWorld.exe
Hello World!

安装提示

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

Please note that I have added LLVM to my search paths during setup:

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

And you can crosscheck the available "Platform Toolsets" in any VS project's property page:

参考资料

  • 什么是-D定义告诉Cmake在哪里可以找到nmake?
  • Clang 链接器?
  • 使用 CMake 在 GCC 和 Clang/LLVM 之间切换

这篇关于如何告诉 CMake 在 Windows 上使用 Clang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何告诉 CMake 在 Windows 上使用 Clang?