【发布时间】:2018-03-15 04:08:51
【问题描述】:
这个问题来自 2017 年,可能已经过时。由于现在可能有更好的解决方案,因此请注意所提供的说明。
亲爱的 C++ 程序员们,
在使用 Visual Studio 工具链在 Windows 上构建一段时间后,我决定试一试 Clang 5。
我安装了 LLVM 5.0.0 二进制文件、Ninja 构建环境、VS 2017 工具和 CMake 3.9.3。最终目标是能够使用 VS Code 编译适用于 Windows 的 C 和 C++ 应用程序,将 CMake 集成作为“IDE”,使用 Clang 将 LLD 作为编译器和链接器。
一个简单程序的编译和执行工作得非常好(screenshot of the respective terminal history)。 Clang 自动检测 VS Tools 目录中的 Windows 标准库并生成可执行输出。
下一步是使用 Ninja (screenshot of ninja.build file and terminal history) 设置一个简单的构建。构建过程按预期工作并生成了一个工作可执行文件,就像以前一样。
当我开始将 CMake 集成到流程中时,问题就开始了。我的期望是 CMake 生成一个 ninja 构建文件并运行它,对吗? 我尝试了以下 CMakeLists 文件
cmake_minimum_required(VERSION 3.9)
project(Test)
add_executable(Test main.c)
并使用cmake -G Ninja 调用CMake。
结果输出令人失望,我理解的不够深入,无法自己分别解决问题。
-- The C compiler identification is Clang 5.0.0
-- The CXX compiler identification is Clang 5.0.0
-- Check for working C compiler: C:/Meine_Programme/LLVM/bin/clang.exe
-- Check for working C compiler: C:/Meine_Programme/LLVM/bin/clang.exe -- broken
CMake Error at C:/Meine_Programme/CMake/share/cmake-3.9/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler "C:/Meine_Programme/LLVM/bin/clang.exe" is not able to
compile a simple test program.
It fails with the following output:
Change Dir: D:/Dateien/Downloads/Test/CMakeFiles/CMakeTmp
Run Build Command:"C:/Meine_Programme/Ninja_Build/ninja.exe" "cmTC_eeb5c"
[1/2] Building C object CMakeFiles\cmTC_eeb5c.dir\testCCompiler.c.obj
FAILED: CMakeFiles/cmTC_eeb5c.dir/testCCompiler.c.obj
C:\Meine_Programme\LLVM\bin\clang.exe /nologo /DWIN32 /D_WINDOWS /W3 /MDd
/Zi /Ob0 /Od /RTC1 /showIncludes
/FoCMakeFiles\cmTC_eeb5c.dir\testCCompiler.c.obj
/FdCMakeFiles\cmTC_eeb5c.dir\ -c testCCompiler.c
clang.exe: error: no such file or directory: '/nologo'
clang.exe: error: no such file or directory: '/DWIN32'
clang.exe: error: no such file or directory: '/D_WINDOWS'
clang.exe: error: no such file or directory: '/W3'
clang.exe: error: no such file or directory: '/MDd'
clang.exe: error: no such file or directory: '/Zi'
clang.exe: error: no such file or directory: '/Ob0'
clang.exe: error: no such file or directory: '/Od'
clang.exe: error: no such file or directory: '/RTC1'
clang.exe: error: no such file or directory: '/showIncludes'
clang.exe: error: no such file or directory:
'/FoCMakeFiles\cmTC_eeb5c.dir\testCCompiler.c.obj'
clang.exe: error: no such file or directory:
'/FdCMakeFiles\cmTC_eeb5c.dir\'
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)
-- Configuring incomplete, errors occurred!
See also "D:/Dateien/Downloads/Test/CMakeFiles/CMakeOutput.log".
See also "D:/Dateien/Downloads/Test/CMakeFiles/CMakeError.log".
我猜这个问题与 CMake 调用 clang 与 VS 样式选项使用斜线而不是前面的减号有关,就像 clang 需要的那样。
谢谢你们帮我,我很感激 :-)
如果您需要更多信息,请给我留言。
回答弗洛里安人的帖子
我尝试了 Florians 命令,但省略了 ninja 的路径以获得更短的符号,结果证明它工作得很好。
cmake -E env LDFLAGS="-fuse-ld=lld" cmake -H. -G Ninja -Bbuild -DCMAKE_C_COMPILER:PATH="C:\MeineProgramme\LLVM\bin\clang.exe" -DCMAKE_CXX_COMPILER:PATH="C:\MeineProgramme\LLVM\bin\clang++.exe" -DCMAKE_C_COMPILER_ID="Clang" -DCMAKE_CXX_COMPILER_ID="Clang" -DCMAKE_SYSTEM_NAME="Generic"
CMake 生成了一个 ninja 构建文件。
我运行ninja all 将可执行文件构建为Test。我将它重命名为Test.exe,程序运行愉快。到目前为止......成功!但比我预想的要复杂得多。
【问题讨论】:
-
不是您实际问题的答案,但可能会有所帮助:stackoverflow.com/a/38174328/2436175
-
@Antonio 感谢您的回复,但 CMake 似乎无法为 Ninja 指定工具集。我试过
cmake -G Ninja -T LLVM-VS2017,但它告诉我,相应的生成器不支持工具集的规范,太糟糕了:-(如果它有效,我不会感到惊讶,因为如上所述,我确实有所有需要除 VS Studio 外安装的工具