使用 CMake、Clang 和 Ninja 在 Windows 上构建 C++ 项目
问题描述
我目前在 Windows 上安装了 cmake、clang 和 ninja.我正在尝试使用 CMake 生成 ninja 构建文件来编译一个非常简单的 hello world 程序.
I currently have cmake, clang and ninja installed on windows. I am trying to use CMake to generate a ninja build file to compile a very simple hello world program.
我的 CMakeLists.txt 看起来像这样:
My CMakeLists.txt looks like this:
main.cpp
是一个简单的 hello world 程序.
main.cpp
is a simple hello world program.
在命令行上我运行这个:cmake -G Ninja ..
并且我得到以下错误:
On the command line I run this: cmake -G Ninja ..
and I get the following errors:
CMakeError.log
文件如下所示:
看来 cmake 正在尝试使用 Windows 选项 /nologo
和 /showIncludes
来测试 clang.我不知道如何告诉 cmake 传递正确的参数.
It appears that cmake is trying to test clang with windows options /nologo
and /showIncludes
. I cannot figure out how to tell cmake to pass the proper arguments.
FWIW 我运行的是 64 位 Windows 7
FWIW I'm running 64bit Windows 7
所以我查看了内置的 cmake 文件,我发现 CMakeClDeps.cmake
文件是添加 /nologo/showIncludes
选项的罪魁祸首.看来,如果我将 Clang 设置为编译器,那么 cmake 认为 Visual Studio 是编译器(它将 MSVC_C_ARCHITECTURE_ID 设置为 x86).
So I looked through the built in cmake files and I found that the CMakeClDeps.cmake
file was the culprit for adding the /nologo /showIncludes
options. It appears that if I set Clang as the compiler then cmake thinks that visual studio is the compiler (it sets MSVC_C_ARCHITECTURE_ID to x86).
我删除了 CMakeDetermineCompilerId.cmake
中设置 MSVC_C_ARCHITECTURE_ID
的行,再次尝试后,我收到以下错误:
I removed the line in CMakeDetermineCompilerId.cmake
that sets MSVC_C_ARCHITECTURE_ID
and after trying again I get the following errors:
推荐答案
不知道它是否有帮助,但我遇到了同样的错误.现在我可以在 Windows 上使用 clang(3.7.1)/ninja(1.6)/cmake(3.4.1) 进行编译,在构建目录中执行以下操作:
Don't know if it can be helpful but I had the same error. Now I can compile with clang(3.7.1)/ninja(1.6)/cmake(3.4.1) on Windows performing the following actions in a build directory:
- 加载相关的 vcvarsXX.bat 文件(例如
"
)VCvcvarsall.bat" x86 - 将 CC 和 CXX 设置为
clang-cl
(而不是clang
和clang++
) - 运行
cmake -G Ninja
- 运行
cmake --build .
- load the relevant vcvarsXX.bat file (e.g.
"<Your Visual Studio location>VCvcvarsall.bat" x86
) - set both CC and CXX to
clang-cl
(instead ofclang
andclang++
) - run
cmake -G Ninja <project>
- run
cmake --build .
这篇关于使用 CMake、Clang 和 Ninja 在 Windows 上构建 C++ 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!