Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
like image 687
maxik Avatar asked Oct 11 '18 13:10

maxik


People also ask

Does Ninja use CMake?

Working with Ninja using CMakeA build generator system like CMake can be used to create the input files for Ninja. To showcase working with Ninja using CMake, let us build Ninja using CMake with Ninja as the backend. Make sure that ninja.exe created by the bootstrap version is in the path.

Is Ninja faster than CMake?

For incremental builds, Make is significantly slow. Ninja is faster and helps developers spend less time on building software. This becomes a driving force for large projects such as Google Chrome. In general, performance of Ninja is much better than Make.

What is Clang CL?

● The driver provides convenience and compatibility ● clang-cl is a cl.exe compatible driver mode for clang ● It understands the environment, the flags, and the tools ● Integrates with Visual Studio ● /fallback allows bring-up of large projects.

What is Ninja vs CMake?

CMake is a build generator while Ninja is a build tool. CMake can use any build tool like Make or Ninja while Ninja has to use CMake as its build generator which is compulsory. CMake was developed in 2000 by Kitware.


1 Answers

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.

like image 90
lav Avatar answered Sep 22 '22 02:09

lav