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

zlhcx6iw  于 2023-06-07  发布在  Windows
关注(0)|答案(4)|浏览(433)

这个问题是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 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使用VS样式选项调用clang有关,使用slash而不是像clang要求的那样前面加减号。
谢谢你们的帮助,我很感激:-)
如果你需要进一步的信息,请给我留言。

回复Florian帖子

我尝试了Florians命令,但为了更短的符号而省略了忍者的路径,结果它工作得很好。

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,程序运行得很愉快。到目前为止...成功!但比我想象的要复杂得多。

5cnsuln7

5cnsuln71#

受@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"

以下是一些背景信息:

  • 我用LDFLAGS环境变量注入了链接器标志

参见Passing compiler options cmake

  • 我减少了PATH环境变量,使其只指向ninja的位置,因为CMake选择了我的MinGW工具链(我不希望将其包含在构建过程中)

关于Environment variable used by CMake to detect Visual C++ compiler tools for Ninja

  • 定义编译器id“绕过对工作编译器和基本编译器信息测试的检查”

参见过时,但有时有用CMakeForceCompiler module

  • 我将CMAKE_SYSTEM_NAME设置为Generic,以避免CMake添加任何额外的特定于平台的编译器/链接器标志

参见How to partially disabling cmake C/C++ custom compiler checking
目前看来,你必须绕过许多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
wd2eg0qa

wd2eg0qa2#

我终于找到了一种方法来使用我喜欢的工具,以一种让我高兴的方式。它并不完美,但它比Florian将系统名称设置为 * Generic *(我已经使用了一段时间)的方法更好
我首先设置了VS Code,以使用VS开发人员终端作为其标准终端。我通过在VS代码首选项中添加以下行来实现这一点

"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构建工具是资源编译器,我现在不使用。
到目前为止,我认为成功。
如果您有进一步的问题,请不要犹豫与我联系或在下面发表评论。

holgip5t

holgip5t3#

我在尝试同时使用clang cmake和msvc 2017时也遇到了类似的问题。至少对于一个非常简单的测试项目,我能够让所有东西都运行起来,但是我对这些东西很陌生,所以也许我的解决方案不能解决您的问题。
无论如何。据我所知,你应该在VS中使用clang-cl.exe而不是clang.exe。然而,由于一些关于x86与x64库不兼容的链接器问题,在x86配置中构建仍然失败。
因此,这是我在VS 2017中构建的x64和x86配置的解决方案。
1.从http://releases.llvm.org/download.html下载并安装两者windows clang/llvm安装程序。您不必将它们添加到路径中,因为我们稍后将显式指定路径。
1.创建一个包含CMakeLists.txt的文件夹,并通过Open Folder对话框在VS中打开该文件夹。
1.在CMake菜单中,选择Change CMake Settings > CMakeLists.txt。这将生成一个包含所有构建配置设置的CMakeSettings.json
1.在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配置而不会出错。

vhmi4jdf

vhmi4jdf4#

这是个老问题了但是。。
关于使用它们的当前版本,clang,ninja和cmake,如果你直接在终端中构建,你可以在PowerShell中使用它:

cmake -B build -S . -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files/LLVM/bin/clang.exe" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files/LLVM/bin/clang.exe" -DCMAKE_LINKER:FILEPATH="C:/Program Files/LLVM/bin/lld-link.exe" -G"Ninja" -DCMAKE_EXPORT_COMPILE_COMMANDS=1

不需要指定任何标志或CMAKE_<LANG>_COMPILER_ID

测试:

Clang 16.0.4、Ninja 1.11.1、Powershell 7.3.4和CMake 3.26

相关问题