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

时间:2022-10-17 07:07:44

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.

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

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

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

C:\Program Files\LLVM\bin\clang.exe
C:\Program Files\LLVM\bin\clang++.exe

It is also installed on my PATH:

它也安装在我的道路上:

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

I have two questions:

我有两个问题:

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

1 个解决方案

#1


25  

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

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

The latest CMake 3.6 builds do have several integrated supported Clang build environments on Windows (e.g. Visual Studio, Cygwin; see Release Notes).

最新的CMake 3.6构建在Windows上有几个集成支持的Clang构建环境(例如Visual Studio, Cygwin;请参阅发行说明)。

I've just run a successful test with

我刚刚做了一个成功的测试。

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

所有安装到它们的标准路径,并在全局路径环境中使用它们的bin目录。

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.

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

CMakeLists.txt

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 Console

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!

Installation Hints

安装提示

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

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

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

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

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

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

References

引用

#1


25  

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

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

The latest CMake 3.6 builds do have several integrated supported Clang build environments on Windows (e.g. Visual Studio, Cygwin; see Release Notes).

最新的CMake 3.6构建在Windows上有几个集成支持的Clang构建环境(例如Visual Studio, Cygwin;请参阅发行说明)。

I've just run a successful test with

我刚刚做了一个成功的测试。

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

所有安装到它们的标准路径,并在全局路径环境中使用它们的bin目录。

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.

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

CMakeLists.txt

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 Console

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!

Installation Hints

安装提示

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

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

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

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

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

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

References

引用