我有一个使用 CMake 构建的 C++ 项目。我通常在 OSX 上构建,但现在我正在尝试让 Windows 版本也能工作。出于兼容性原因,我想在 Windows 上使用 Clang。
我从 LLVM 安装了预编译的 Clang 3.8 二进制文件:
C:\Program Files\LLVM\bin\clang.exe
C:\Program Files\LLVM\bin\clang++.exe
它也安装在我的路径上:
>clang++
clang++.exe: error: no input files
我有两个问题:
- 我如何告诉 CMake 使用
clang++
当我打电话时cmake --build
?
- 在构建之前如何检查 CMake 配置了哪个编译器?
除了 Clang 编译器本身之外,您还需要 Windows 的构建/链接环境。
最新的 CMake 3.6 构建确实在 Windows 上有几个集成的受支持的 Clang 构建环境(例如 Visual Studio、Cygwin;请参阅发行说明 https://cmake.org/cmake/help/v3.6/release/3.6.html).
我刚刚成功进行了测试
-
LLVM-3.9.0-r273898-win32.exe http://llvm.org/pre-releases/win-snapshots/LLVM-3.9.0-r273898-win32.exe from http://llvm.org/builds/ http://llvm.org/builds/
-
cmake-3.6.0-rc4-win64-x64.msi https://cmake.org/files/v3.6/cmake-3.6.0-rc4-win64-x64.msi from https://cmake.org/download/ https://cmake.org/download/
- 微软VS2015社区版版本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 添加到我的搜索路径中:
您可以在任何 VS 项目的属性页中交叉检查可用的“平台工具集”:
参考
- 告诉 Cmake 在哪里找到 nmake 的 -D 定义是什么? https://stackoverflow.com/questions/38106848/what-is-the-d-define-to-tell-cmake-where-to-find-nmake
- Clang 的链接器? https://stackoverflow.com/questions/3925409/linker-for-clang
- 使用 CMake 在 GCC 和 Clang/LLVM 之间切换 https://stackoverflow.com/questions/7031126/switching-between-gcc-and-clang-llvm-using-cmake
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)