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

我有两个问题:

>当我打电话给cmake –build时,如何告诉CMake使用clang?
>如何在构建CMake配置的编译器之前检查?

解决方法

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

最新的CMake 3.6版本在Windows上有几个集成支持的Clang构建环境(例如Visual Studio,Cygwin;见Release Notes).

我刚刚用一个成功的测试

> LLVM-3.9.0-r273898-win32.exehttp://llvm.org/builds/
> cmake-3.6.0-rc4-win64-x64.msihttps://cmake.org/download/
> Microsoft VS2015 Community Edition版本14.0.23107.0

全部安装到其标准路径,其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)

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

参考

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

相关文章

首先GDB是类unix系统下一个优秀的调试工具, 当然作为debug代...
1. C语言定义1个数组的时候, 必须同时指定它的长度.例如:int...
C++的auto关键字在C+⬑新标准出来之前基本...
const关键字是用于定义一个不该被改变的对象,它的作用是告诉...
文章浏览阅读315次。之前用C语言编过链表,这几天突然想用C+...
文章浏览阅读219次。碰到问题就要记录下来,防止遗忘吧。文章...