【问题标题】:CMake building for Windows (clang-cl) using Ninja Generator使用 Ninja Generator 为 Windows (clang-cl) 构建 CMake
【发布时间】:2019-03-16 13:54:53
【问题描述】:

我正在尝试使用 CMake 作为主要构建工具在 Windows 机器上构建一个简单的应用程序。一旦在项目上调用 CMake,配置阶段就会出错:

> cmake -H. -G Ninja -Bbuild -DCMAKE_C_COMPILER:PATH="C:\Program Files\LLVM\bin\clang-cl.exe" -DCMAKE_CXX_COMPILER:PATH="C:\Program Files\LLVM\bin\clang-cl.exe"

-- The C compiler identification is Clang 7.0.0
-- The CXX compiler identification is Clang 7.0.0
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang-cl.exe
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang-cl.exe --broken
CMake Error at C:/Program Files/CMake/share/cmake-3.12/Modules/CMakeTestCCompile
r.cmake:52 (message):
  The C compiler

    "C:/Program Files/LLVM/bin/clang-cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/mak/Desktop/cmake-test/build/CMakeFiles/CMakeTmp

    Run Build Command:"C:/Qt/Tools/QtCreator/bin/ninja.exe" "cmTC_f5485"
    [1/2] Building C object CMakeFiles\cmTC_f5485.dir\testCCompiler.c.obj
    [2/2] Linking C executable cmTC_f5485.exe
    FAILED: cmTC_f5485.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_f5485.dir --manifests  -- CMAKE_LINKER-NOTFOUND  /nologo CMakeFiles\cmTC_f5485.dir\testCCompiler.c.obj  /out:cmTC_f5485.exe /implib:cmTC_f5485.lib /pdb:cmTC_f5485.pdb /version:0.0  /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    RC Pass 1: command "rc /foCMakeFiles\cmTC_f5485.dir/manifest.res CMakeFiles\cmTC_f5485.dir/manifest.rc" failed (exit code 0) with the following output:
    The system cannot find the given file
    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)

我通过网络阅读了很多内容,但任何建议的解决方案都没有解决我的问题。到目前为止我发现的是一个类似但可能已经过时的solution 对我不起作用的相同问题,因为 Ninja 无法构建可执行文件:

> ninja all
[1/2] Building CXX object CMakeFiles/minimal.dir/main.cpp.obj
FAILED: CMakeFiles/minimal.dir/main.cpp.obj
C:\PROGRA~1\LLVM\bin\clang-cl.exe     -MD -MT CMakeFiles/minimal.dir/main.cpp.obj -MF CMakeFiles\minimal.dir\main.cpp.obj.d -o CMakeFiles/minimal.dir/main.cpp.obj -c ../main.cpp
clang-cl.exe: warning: unknown argument ignored in clang-cl: '-MF' [-Wunknown-argument]
clang-cl.exe: error: no such file or directory: 'CMakeFiles/minimal.dir/main.cpp.obj'
clang-cl.exe: error: no such file or directory: 'CMakeFiles\minimal.dir\main.cpp.obj.d'
ninja: build stopped: subcommand failed.

在此错误之前,CMake 配置正确,但所有编译器 ABI 信息检测均失败 - 但 CMake 恢复时没有错误。有someother 的问题也没有帮助。

官方documentation说很简单,其实不然。

所以:如何使用带有 Ninja 生成器和 Clang 作为编译器的 CMake 构建一个简单 C++ 项目?我尽量避免安装 Visual Studio,但如果生成的二进制文件与 MSVC 构建二进制文件兼容,那就太好了。

版本:

  • CMake 3.12.2
  • 忍者 1.8.2
  • Clang 7.0.0

示例:这是我正在使用的小示例:

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(minimal)
add_executable(${PROJECT_NAME} main.cpp)

main.cpp

#include <stdio.h>

int main(void)
{
  printf("Hello World!\n");
  return 0;
}

【问题讨论】:

  • 假设“Das System kann die angegebene Datei nicht finden”类似于“系统找不到指定的路径”,错误是关于错过rc 实用程序。另外,CMAKE_LINKER-NOTFOUND 表示 CMake 未能检测到链接器。
  • 你是完全正确的。 (很抱歉错过了这个翻译。)除了所有其他定义之外,我还提供了 CMAKE_LINKERCMAKE_RC_COMPILER 但它也不起作用。输出几乎相同。
  • 您能否确认您正在从 Visual Studio 命令行终端配置项目?
  • 我不使用也不打算使用任何 Visual Studio 工具。在重新阅读文档时,可能会得出这样的结论:这是一种要求。你能证实这一点吗?使用忍者生成器的部分没有直接说。
  • @Tsyvarev 我注意到在手动设置CMAKE_LINKER 时,一旦我运行配置(因此失败),此变量就会设置为CMAKE_LINKER-NOTFOUND。如何正确设置链接器?

标签: cmake ninja clang-cl


【解决方案1】:

要使用 clang-cl 编译,需要在加载 MSVC 环境的情况下运行 cmake(使用 vcvarsall.bat)。否则它会尝试使用 GCC 兼容性选项。 只安装Build Tools就足够了。

【讨论】:

    猜你喜欢
    • 2018-03-15
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多