【问题标题】:Building with CMake, Ninja and Clang on Windows在 Windows 上使用 CMake、Ninja 和 Clang 构建
【发布时间】: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 外安装的工具

标签: windows cmake clang ninja


【解决方案1】:

受@Unspongeful 的"Ways to Compile with Clang on Windows" 博客文章的启发,经过一些扩展测试,以下命令行对我有用(是的,这是一个大命令,为了更好的可读性,我只是分成几行):

> 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

【讨论】:

  • 嗯……这个命令比我预期的要长得多。让我试试你的建议,我会报告的。非常感谢您的广泛研究人员。
  • @Simon 欢迎您。是的,将所有这些放入 CMake 工具链文件中可能会更好/更容易。如果您已经验证了上面的命令行,我可以添加一个工具链方法以便于处理(例如来自@MatthewHungerford 的this blog post)。
  • 我在 CMakes Gitlab 上开始了一个问题。如果你想看看...gitlab.kitware.com/cmake/cmake/issues/17329#
  • 我更新了我的问题并回复了您的帖子。您能否向我提供其他信息,即单个 CMakeLists.txt?
  • @Simon 我会在接下来的几天里添加更多信息。关于ninja 的路径只有一件事。因为它是PATH environment variable,所以我们正在覆盖它,在你的情况下它只是PATH="C:\MeineProgramme\Ninja\bin"(没有可执行文件)。
【解决方案2】:

我终于找到了一种让我满意的方式来使用我喜欢的工具。它并不完美,但它比 Florians 方法更好地将系统名称设置为 Generic(我已经使用了一段时间了)

我首先将 VS Code 设置为使用 VS 开发人员终端作为其标准终端。我通过将以下行添加到 VS Code 首选项来做到这一点

"terminal.integrated.shell.windows": "C:\\MeineProgramme\\Visual_Studio\\2017\\BuildTools\\Common7\\Tools\\LaunchDevCmd.bat"

在 VS Code 中启动终端后,我需要调用相应的批处理文件来设置所需的环境变量(在我的情况下为 vcvars64.bat)。这些可以在

中找到
C:\MeineProgramme\Visual_Studio\2017\BuildTools\VC\Auxiliary\Build

导航到我的构建目录后,我使用以下选项运行 CMake

cmake .. -G Ninja -DCMAKE_CXX_COMPILER:PATH="C:\MeineProgramme\LLVM\bin\clang-cl.exe" -DCMAKE_LINKER:PATH="C:\MeineProgramme\LLVM\bin\lld-link.exe"

这鼓励 CMake 使用我安装的所有 LLVM 工具。不仅是clanglld(确保使用支持/ 引导的选项的lld-link),还有llvm-arllvm-ranlib。唯一使用的 MS 构建工具是我目前不使用的资源编译器。

我认为到目前为止是成功的。

如果您有其他问题,请随时与我联系或在下方发表评论。

【讨论】:

  • 您也可以使用位于C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\VCx64 Native Tools Command Prompt for VS 2017 快捷方式。这会自动为您调用vcvars64.bat
  • Ninja 为此需要哪些变量?我希望能够在我自己的cmd 窗口上模仿这个。
  • @Royi 我不明白。你的意思是?这适用于普通 cmd,但您要么必须在路径上有 Visual_Studio\2017\BuildTools\VC\Auxiliary\Build,要么使用完整路径引用 vcvars64.bat。在每个环境中对 cmake 的调用都是相同的。
  • 你说this encourages CMake to use all my installed LLVM tools.。即在vcvars64.bat 中设置的一些系统环境设置实现了这一点。我问你具体是哪些(因为它增加了很多)。
  • 嗯,我不知道,但 LLVM 需要设置包含和库变量才能在构建期间找到所需的文件。为什么不直接执行环境脚本?
【解决方案3】:

我在尝试同时使用 clang cmake 和 msvc 2017 时遇到了类似的问题。至少对于一个非常简单的测试项目,我能够让一切运行起来,但我对这些东西很陌生,所以也许我的解决方案无法解决你的问题。

无论如何。据我所知,您应该在 VS 中使用 clang-cl.exe 而不是 clang.exe。但是,由于一些与 x86 与 x64 库不兼容有关的链接器问题,在 x86 配置中构建对我来说仍然失败。

所以这是我在 VS 2017 中构建的同时获得 x64 和 x86 配置的解决方案。

  1. http://releases.llvm.org/download.html 下载并安装 BOTH windows clang/llvm 安装程序。您不必将它们添加到路径中,因为我们稍后会明确指定路径。
  2. 使用CMakeLists.txt 创建一个文件夹,然后通过Open Folder 对话框在VS 中打开它。
  3. CMake 菜单中,选择Change CMake Settings &gt; CMakeLists.txt。这将生成一个CMakeSettings.json,其中包含所有构建配置的设置。
  4. cmakeCommandArgs 中为所有配置指定x64/x86 cmake 编译器的路径。我的看起来像这样:

    {    // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
    
    "configurations": [
        {
            "name": "x86-Debug",
            "generator": "Ninja",
            "configurationType": "Debug",
            "inheritEnvironments": [ "msvc_x86" ],
            "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
            "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
            "cmakeCommandArgs": "-D CMAKE_CXX_COMPILER=D:/windows/LLVM5_x86/bin/clang-cl.exe",
            "buildCommandArgs": "-v",
            "ctestCommandArgs": ""
        },
        {
            "name": "x86-Release",
            "generator": "Ninja",
            "configurationType": "RelWithDebInfo",
            "inheritEnvironments": [ "msvc_x86" ],
            "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
            "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
            "cmakeCommandArgs": "-D CMAKE_CXX_COMPILER=D:/windows/LLVM5_x86/bin/clang-cl.exe",
            "buildCommandArgs": "-v",
            "ctestCommandArgs": ""
        },
        {
            "name": "x64-Debug",
            "generator": "Ninja",
            "configurationType": "Debug",
            "inheritEnvironments": [ "msvc_x64" ],
            "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
            "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
            "cmakeCommandArgs": "-D CMAKE_CXX_COMPILER=D:/windows/LLVM5/bin/clang-cl.exe",
            "buildCommandArgs": "-v",
            "ctestCommandArgs": ""
        },
        {
            "name": "x64-Release",
            "generator": "Ninja",
            "configurationType": "RelWithDebInfo",
            "inheritEnvironments": [ "msvc_x64" ],
            "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
            "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
            "cmakeCommandArgs": "-D CMAKE_CXX_COMPILER=D:/windows/LLVM5/bin/clang-cl.exe",
            "buildCommandArgs": "-v",
            "ctestCommandArgs": ""
        }
    ]
    

    }

现在您应该能够构建 x64 和 x86 配置而不会出错。

【讨论】:

  • 感谢您的努力 florestan,但我不确定您是否正确理解了我的问题。我不是要求 VS 集成有问题。我想尽可能避免使用 VS,只安装 VS 工具来访问 Windows 标准库和头文件。我的问题是 CMake 没有将 clang 或 clang-cl 检测为以 Ninja 作为生成器的有效编译器。
  • 好的,我把你的问题搞错了,抱歉。有趣的是,即使我的 VS 也无法使用正确的编译器,除非如上所述明确指定它。我在没有 VS 的情况下玩了一点,我只能确认你的问题 :-( 在这种情况下,CMake 似乎完全忽略了 CMAKE_CXX_COMPILER 参数。
猜你喜欢
  • 2014-04-30
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
相关资源
最近更新 更多