Skip to content

clang-cl Ninja try_compile error

Cmake try_compile may cause error when use clang-cl and Ninja

I hava tried clang-cl compiles fine on windows cmd(not the VsDevCmd.bat provided by Microsoft) 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;
}

View specific commands

clang -v test.c

The Command Result

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"

But when use cmake with Ninja, The configure will crash when execute try_compile Here are the cmake file CMakeLists.txt

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(cmake-demo LANGUAGES C)
add_executable(hello test.c)

View specific commands

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.

It is obvious that the commands generated by try_compile at the link stage are quite different from those directly via clang-cl, Whether the actual link command can be consistent, this eliminates the need to use the tools VsDevCmd.bat provided by msvc

Edited by ziqi chen
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information