7

我正在尝试使用 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)

我通过网络阅读了很多内容,但任何建议的解决方案都没有解决我的问题。到目前为止,我发现的是一个类似但可能已经过时的相同问题的解决方案,这对我不起作用,因为 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 恢复没有错误。还有一些 其他问题也没有帮助。

官方文档说这很简单,但实际上并非如此。

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

版本:

  • CMake 3.12.2
  • 忍者 1.8.2
  • 铿锵声 7.0.0

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

CMakeLists.txt

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

主文件

#include <stdio.h>

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

1 回答 1

3

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

于 2018-11-14T07:32:37.447 回答