ninja: windows cmake use clang-cl with Ninja during configuration process crashed

I found a compilation issue, which I checked and found to be ninja related. More specifically: All use cases use the command line environment.
There are command line two enviroment

  • The command line of windows : which works fine on other generator,but crashed when use ninja
  • VsDevCmd.bat provided by Microsoft: all case works fine More specifically:
  1. First I try to compile directly by clang-cl in cmd, and It works fine Here are the c file test.c
#include <stdio.h>
int main() {
    printf("%s","hello world");
    return 1;
}
clang -v test.c
clang version 15.0.7 (https://github.com/llvm/llvm-project.git 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: D:\Clang-llvm\bin
 "D:\\Clang-llvm\\bin\\clang.exe" -cc1 -triple x86_64-pc-windows-msvc19.34.31937 -emit-obj -mrelax-all -mincremental-linker-compatible --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -v "-fcoverage-compilation-dir=C:\\Users\\chenziqi\\Desktop\\cpp\\cmake-study" -resource-dir "D:\\Clang-llvm\\lib\\clang\\15.0.7" -internal-isystem "D:\\Clang-llvm\\lib\\clang\\15.0.7\\include" -internal-isystem "D:\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\include" -internal-isystem "D:\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\atlmfc\\include" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\ucrt" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\shared" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\um" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\winrt" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\cppwinrt" "-fdebug-compilation-dir=C:\\Users\\chenziqi\\Desktop\\cpp\\cmake-study" -ferror-limit 19 -fmessage-length=120 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.34.31937 -fdelayed-template-parsing -fcolor-diagnostics -faddrsig -o "C:\\Users\\chenziqi\\AppData\\Local\\Temp\\main-d0748c.o" -x c main.c
clang -cc1 version 15.0.7 based upon LLVM 15.0.7 default target x86_64-pc-windows-msvc
ignoring nonexistent directory "D:\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\atlmfc\include"
#include "..." search starts here:
#include <...> search starts here:
 D:\Clang-llvm\lib\clang\15.0.7\include
 D:\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\include
 D:\Windows Kits\10\Include\10.0.22621.0\ucrt
 D:\Windows Kits\10\Include\10.0.22621.0\shared
 D:\Windows Kits\10\Include\10.0.22621.0\um
 D:\Windows Kits\10\Include\10.0.22621.0\winrt
 D:\Windows Kits\10\Include\10.0.22621.0\cppwinrt
End of search list.
 "D:\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\bin\\Hostx64\\x64\\link.exe" -out:a.exe -defaultlib:libcmt -defaultlib:oldnames "-libpath:D:\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\lib\\x64" "-libpath:D:\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\atlmfc\\lib\\x64" "-libpath:D:\\Windows Kits\\10\\Lib\\10.0.22621.0\\ucrt\\x64" "-libpath:D:\\Windows Kits\\10\\Lib\\10.0.22621.0\\um\\x64" -nologo "C:\\Users\\chenziqi\\AppData\\Local\\Temp\\main-d0748c.o"

Obviously clang-cl compiles directly

  1. I create a cmake project named cmake-test, below are some details:
//CMakeLists.txt
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(cmake-demo LANGUAGES C)
add_executable(hello main.c)
//the command
cmake -G"Visual Studio 17 2022" -B build -DCMAKE_C_COMPILER=clang-cl

The command result

-- The C compiler identification is MSVC 19.34.31937.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/chenziqi/Desktop/cpp/cmake-study/build

Also works fine

  1. But when configure by Ninja, the program crashed //the command
cmake -G"Ninja" -B build -DCMAKE_C_COMPILER=clang-cl -DCMAKE_C_FLAGS=-v

The command result

-- The C compiler identification is Clang 15.0.7 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: D:/Clang-llvm/bin/clang-cl.exe
-- Check for working C compiler: D:/Clang-llvm/bin/clang-cl.exe - broken
CMake Error at D:/Cmake/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message):
  The C compiler

    "D:/Clang-llvm/bin/clang-cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/chenziqi/Desktop/cpp/cmake-study/build/CMakeFiles/CMakeScratch/TryCompile-63h9c2

    Run Build Command(s):D:/Ninja/bin/ninja.exe cmTC_20407 && [1/2] Building C object CMakeFiles\cmTC_20407.dir\testCCompiler.c.obj
    clang version 15.0.7 (https://github.com/llvm/llvm-project.git 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
    Target: x86_64-pc-windows-msvc
    Thread model: posix
    InstalledDir: D:\Clang-llvm\bin
     (in-process)
     "D:\\Clang-llvm\\bin\\clang-cl.exe" -cc1 -triple x86_64-pc-windows-msvc19.34.31937 -emit-obj -mrelax-all -mincremental-linker-compatible --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name testCCompiler.c -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -mllvm -x86-asm-syntax=intel -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -D_DEBUG -D_MT -D_DLL --dependent-lib=msvcrtd --dependent-lib=oldnames --show-includes -sys-header-deps -stack-protector 2 -fms-volatile -fdiagnostics-format msvc -gno-column-info -gcodeview -debug-info-kind=constructor -v "-fcoverage-compilation-dir=C:\\Users\\chenziqi\\Desktop\\cpp\\cmake-study\\build\\CMakeFiles\\CMakeScratch\\TryCompile-63h9c2" -resource-dir "D:\\Clang-llvm\\lib\\clang\\15.0.7" -internal-isystem "D:\\Clang-llvm\\lib\\clang\\15.0.7\\include" -internal-isystem "D:\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\include" -internal-isystem "D:\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\atlmfc\\include" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\ucrt" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\shared" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\um" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\winrt" -internal-isystem "D:\\Windows Kits\\10\\Include\\10.0.22621.0\\cppwinrt" -O0 "-fdebug-compilation-dir=C:\\Users\\chenziqi\\Desktop\\cpp\\cmake-study\\build\\CMakeFiles\\CMakeScratch\\TryCompile-63h9c2" -ferror-limit 19 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.34.31937 -fdelayed-template-parsing -fno-inline -faddrsig "-object-file-name=C:\\Users\\chenziqi\\Desktop\\cpp\\cmake-study\\build\\CMakeFiles\\CMakeScratch\\TryCompile-63h9c2\\CMakeFiles\\cmTC_20407.dir\\testCCompiler.c.obj" -o "CMakeFiles\\cmTC_20407.dir\\testCCompiler.c.obj" -x c "C:\\Users\\chenziqi\\Desktop\\cpp\\cmake-study\\build\\CMakeFiles\\CMakeScratch\\TryCompile-63h9c2\\testCCompiler.c"
    clang -cc1 version 15.0.7 based upon LLVM 15.0.7 default target x86_64-pc-windows-msvc
    ignoring nonexistent directory "D:\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\atlmfc\include"
    #include "..." search starts here:
    #include <...> search starts here:
     D:\Clang-llvm\lib\clang\15.0.7\include
     D:\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\include
     D:\Windows Kits\10\Include\10.0.22621.0\ucrt
     D:\Windows Kits\10\Include\10.0.22621.0\shared
     D:\Windows Kits\10\Include\10.0.22621.0\um
     D:\Windows Kits\10\Include\10.0.22621.0\winrt
     D:\Windows Kits\10\Include\10.0.22621.0\cppwinrt
    End of search list.
    [2/2] Linking C executable cmTC_20407.exe
    FAILED: cmTC_20407.exe
    cmd.exe /C "cd . && D:\Cmake\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_20407.dir --rc=D:\Clang-llvm\bin\llvm-rc.exe --mt=CMAKE_MT-NOTFOUND --manifests  -- D:\Clang-llvm\bin\lld-link.exe /nologo CMakeFiles\cmTC_20407.dir\testCCompiler.c.obj  /out:cmTC_20407.exe /implib:cmTC_20407.lib /pdb:cmTC_20407.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 ."
    MT: command "CMAKE_MT-NOTFOUND /nologo /manifest CMakeFiles\cmTC_20407.dir/intermediate.manifest /out:CMakeFiles\cmTC_20407.dir/embed.manifest /notify_update" failed (exit code 0x0) with the following output:
    系统找不到指定的文件。
    ninja: build stopped: subcommand failed.

I observed a difference between the actual link command and compiling it directly from clang-cl, which is available directly from the windows command line

About this issue

Most upvoted comments

Again thank you very much on such faster reply @acesolucoes, Unfortunately still clang++ generates same error, P.S I didn’t use the VS2022 clang, I used latest MinGW build 12.2.0 I used those tools because it doesn’t need to be installed (No Admin rights)

PS F:\tc> cmake . -D CMAKE_C_COMPILER="clang" -DCMAKE_CXX_COMPILER="clang++" -D CMAKE_BUILD_TYPE=Release -G "Ninja" -B .\build\
-- The CXX compiler identification is Clang 16.0.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: D:/m/gcc/bin/clang++.exe
-- Check for working CXX compiler: D:/m/gcc/bin/clang++.exe - broken
CMake Error at D:/m/CMake/share/cmake-3.26/Modules/CMakeTestCXXCompiler.cmake:60 (message):
  The C++ compiler

    "D:/m/gcc/bin/clang++.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: F:/tc/build/CMakeFiles/CMakeScratch/TryCompile-73dfr0

    Run Build Command(s):D:/m/gcc/bin/ninja.exe -v cmTC_20661 && [1/2] D:\m\gcc\bin\clang++.exe    -MD -MT CMakeFiles/cmTC_20661.dir/testCXXCompiler.cxx.obj -MF CMakeFiles\cmTC_20661.dir\testCXXCompiler.cxx.obj.d -o CMakeFiles/cmTC_20661.dir/testCXXCompiler.cxx.obj -c F:/tc/build/CMakeFiles/CMakeScratch/TryCompile-73dfr0/testCXXCompiler.cxx
    [2/2] cmd.exe /C "cd . && D:\m\gcc\bin\clang++.exe   CMakeFiles/cmTC_20661.dir/testCXXCompiler.cxx.obj -o cmTC_20661.exe -Wl,--out-implib,libcmTC_20661.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
    FAILED: cmTC_20661.exe
    cmd.exe /C "cd . && D:\m\gcc\bin\clang++.exe   CMakeFiles/cmTC_20661.dir/testCXXCompiler.cxx.obj -o cmTC_20661.exe -Wl,--out-implib,libcmTC_20661.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
    The system cannot find the path specified.
    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)


-- Configuring incomplete, errors occurred!

If you can find a work around that I would be very thankful, Make takes minimum 3 mins for even small projects

And I have ran the vcvars64.bat also for testing in my VS2022 setup, it fails

I had the same problem. I found the explanation and solution here: https://discourse.cmake.org/t/rc-rc-mt-cmake-mt-notfound/5089/5

To sum up, run vcvarsall.bat before to call the CMake generation: In my case:

%programfiles%\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat

So call your cmake command line generation:

cmake -G "Ninja" -B build3 -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl

It is important to pass both -DCMAKE_C_COMPILER=clang-cl and -DCMAKE_CXX_COMPILER=clang-cl, because if you don’t do that Cmake complains that you are specifying Clang as one compiler ad MSVC as another one, what it is not allowed, giving out an error as result.