C++ 如何告诉 CMake 在 Windows 上使用 Clang?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38171878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I tell CMake to use Clang on Windows?
提问by sdgfsdh
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:
它也安装在我的 PATH 上:
>clang++
clang++.exe: error: no input files
I have two questions:
我有两个问题:
- How do I tell CMake to use
clang++
when I callcmake --build
? - How can I check before building which compiler CMake is configured with?
- 我如何告诉 CMake
clang++
在我打电话时使用cmake --build
? - 在构建 CMake 配置的编译器之前如何检查?
回答by Florian
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
我刚刚运行了一个成功的测试
- LLVM-3.9.0-r273898-win32.exefrom http://llvm.org/builds/
- cmake-3.6.0-rc4-win64-x64.msifrom https://cmake.org/download/
- Microsoft VS2015 Community Edition Version 14.0.23107.0
- LLVM-3.9.0-r273898-win32.exe来自http://llvm.org/builds/
- cmake-3.6.0-rc4-win64-x64.msi来自https://cmake.org/download/
- 微软VS2015社区版14.0.23107.0版
All installed to their standard paths with their bin
directories in the global PATH
environment.
全部安装到其标准路径及其bin
在全局PATH
环境中的目录。
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
视窗控制台
...> 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 添加到我的搜索路径中:
And you can crosscheck the available "Platform Toolsets" in any VS project's property page:
您可以在任何 VS 项目的属性页中交叉检查可用的“平台工具集”:
References
参考
回答by Amin
Follow these instructions:
请按照以下说明操作:
Install choco if you don't have it: https://chocolatey.org/install
如果没有,请安装 choco:https: //chocolatey.org/install
Use A
in the prompts.
A
在提示中使用。
choco install mingw
choco install llvm
choco install cmake
Reset your shell so environment variables are set properly (you can check if bin folders for each are added to your Path).
重置您的 shell,以便正确设置环境变量(您可以检查每个的 bin 文件夹是否已添加到您的路径中)。
Using MinGW
使用 MinGW
Go to your project and run:
转到您的项目并运行:
cmake-gui .
From the upper menu select Tools/Configure
and follow these settings:
从上层菜单中选择Tools/Configure
并遵循以下设置:
Choose MinGW Makefiles and 2nd option (specify native compilers):
选择 MinGW Makefiles 和第二个选项(指定本机编译器):
Give the path to the compilers:
Using Visual Studio
使用 Visual Studio
In some folder install llvm-utils:
在某些文件夹中安装 llvm-utils:
git clone https://github.com/zufuliu/llvm-utils.git
cd llvm-utils/VS2017
.\install.bat
Go to your project and run:
转到您的项目并运行:
cmake-gui .
From the upper menu select Tools/Configure
and follow these settings:
从上层菜单中选择Tools/Configure
并遵循以下设置:
Choose Visual Studio 2019 and 2nd option (specify native compilers). Set
LLVM_v142
for Visual Studio 2019. See herefor others.
选择 Visual Studio 2019 和第二个选项(指定本机编译器)。LLVM_v142
为 Visual Studio 2019设置
。其他请参见此处。