gpt4 book ai didi

windows - 在 Windows 上使用 CMake、Ninja 和 Clang 构建

转载 作者:可可西里 更新时间:2023-11-01 12:19:06 25 4
gpt4 key购买 nike

这个问题来自 2017 年,可能已经过时了。请对提供的说明持保留态度,因为现在可能会提供更好的解决方案。


亲爱的 C++ 程序员们,

在使用 Visual Studio 工具链在 Windows 上构建一段时间后,我决定尝试一下 Clang 5。

我安装了 LLVM 5.0.0 二进制文件、Ninja 构建环境、VS 2017 工具和 CMake 3.9.3。最终目标是能够使用 VS Code 将 CMake 集成作为“IDE”,并使用 Clang 将 LLD 作为编译器和链接器,为 Windows 编译 C 和 C++ 应用程序。

一个简单程序的编译和执行工作得很好(screenshot of the respective terminal history)。 Clang 自动检测 VS 工具目录中的 Windows 标准库并生成可执行输出。

下一步是使用 Ninja ( screenshot of ninja.build file and terminal history) 设置一个简单的构建。构建过程按预期工作并生成了一个工作的可执行文件,就像以前一样。

当我开始将 CMake 集成到流程中时,问题就开始了。我的期望是 CMake 生成一个忍者构建文件并运行它,对吗?我尝试了以下 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 帖子的回复

我尝试了 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 all 以将可执行文件构建为 Test。我把它重命名为Test.exe,程序执行的很开心。至此……成功!!!但比我预期的要复杂得多。

最佳答案

灵感来自 "Ways to Compile with Clang on Windows"来自@Unspongeful 的博客文章,经过一些扩展测试后,以下命令行对我有用(是的,这是一个大命令,我只是分成几行以提高可读性):

> cmake -E env LDFLAGS="-fuse-ld=lld-link" PATH="<path\to\ninja>" 
cmake -H. -G Ninja -Bbuild
-DCMAKE_C_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe"
-DCMAKE_CXX_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe"
-DCMAKE_C_COMPILER_ID="Clang"
-DCMAKE_CXX_COMPILER_ID="Clang"
-DCMAKE_SYSTEM_NAME="Generic"

这里是一些背景信息:

目前看来您必须绕过许多 CMake 的自动检查才能使其正常工作。所以可能与 CMake 团队核实或 raise an issue得到这个场景的正式支持。

Generic 系统的最后一部分可能不是最佳选择,因为它会跳过 Windows 特定设置,如 .exe 后缀。

但这是唯一真正有效的星座:

-- The C compiler identification is Clang
-- The CXX compiler identification is Clang
-- Check for working C compiler: C:/Program Files (x86)/LLVM/bin/clang.exe
-- Check for working C compiler: C:/Program Files (x86)/LLVM/bin/clang.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/bin/clang.exe
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/bin/clang.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: build

关于windows - 在 Windows 上使用 CMake、Ninja 和 Clang 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46553436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com