CMake building for Windows (clang-cl) using Ninja Generator

陌路散爱 提交于 2020-01-03 17:22:05

问题


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)

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.

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.

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.

Versions:

  • CMake 3.12.2
  • Ninja 1.8.2
  • Clang 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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/52760801/cmake-building-for-windows-clang-cl-using-ninja-generator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!