使用忍者生成器为Windows(clang-cl)构建CMake [英] CMake building for Windows (clang-cl) using Ninja Generator

查看:528
本文介绍了使用忍者生成器为Windows(clang-cl)构建CMake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build a simple application on a Windows machine using CMake as the main build tool. Once CMake is invoked on the project the is an error on configuration phase:

> 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无法建立可执行档,因此无法解决这个问题。

I read a lot through the web but my problem was not solved by any proposed solution. What I found so far is a simmilar but maybe outdated solution to the same problem which did not work for me, because Ninja was not able to build the executable:

> 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恢复后没有错误。有一些 其他问题也没有帮助。

Before this error CMake configured properly except that all compilers ABI info detection failed - but CMake resumed without error. There are some other questions out there which did not help either.

正式的文档指出,这很简单,但实际上

The official documentation states it is quite simple but in fact it is not.

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

So: How do I build a simple C++ project using CMake with the Ninja generator and Clang as the compiler? I try to avoid the installation of Visual Studio but it would be great if generated binaries are compatible with MSVC build binaries.

版本:


  • CMake 3.12.2

  • 忍者1.8.2

  • lang 7.0.0

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

Example: Here is the minial example which I am working with:

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;
}


推荐答案

要使用进行编译clang-cl ,必须在加载了MSVC环境的情况下运行 cmake (使用 vcvarsall.bat )。否则,它将尝试使用GCC兼容性选项。
仅安装构建工具就足够了

To compile with clang-cl, it is necessary to run cmake with MSVC environment loaded (use vcvarsall.bat). Otherwise it tries to use GCC compatibility options. It is enough to install just the Build Tools.

这篇关于使用忍者生成器为Windows(clang-cl)构建CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆