simple cmake file failed when generate ninja build script

Hi guys,
I’m trying to setup a simple CMake project without using the MSVC tools

cmake_minimum_required(VERSION 3.13)
project(myproj C)
add_executable(test test.c)

in build directory

cmake .. -G Ninja

failed with

-- The C compiler identification is Clang 18.1.4 with GNU-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: D:/LLVM-LATEST/bin/clang.exe
-- Check for working C compiler: D:/LLVM-LATEST/bin/clang.exe - broken
CMake Error at D:/cmake/share/cmake-3.29/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "D:/LLVM-LATEST/bin/clang.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: 'D:/notes/embeded/tmp/build/CMakeFiles/CMakeScratch/TryCompile-ol011h'

    Run Build Command(s): D:/tools/ninja.exe -v cmTC_bc617
    [1/2] D:\LLVM-LATEST\bin\clang.exe   -O0 -g -Xclang -gcodeview -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -MD -MT CMakeFiles/cmTC_bc617.dir/testCCompiler.c.obj -MF CMakeFiles\cmTC_bc617.dir\testCCompiler.c.obj.d -o CMakeFiles/cmTC_bc617.dir/testCCompiler.c.obj -c D:/notes/embeded/tmp/build/CMakeFiles/CMakeScratch/TryCompile-ol011h/testCCompiler.c
    [2/2] C:\WINDOWS\system32\cmd.exe /C "cd . && D:\LLVM-LATEST\bin\clang.exe -nostartfiles -nostdlib -O0 -g -Xclang -gcodeview -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -Xlinker /subsystem:console  -fuse-ld=lld-link CMakeFiles/cmTC_bc617.dir/testCCompiler.c.obj -o cmTC_bc617.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:cmTC_bc617.lib -Xlinker /pdb:cmTC_bc617.pdb -Xlinker /version:0.0   -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames  && cd ."
    FAILED: cmTC_bc617.exe
    C:\WINDOWS\system32\cmd.exe /C "cd . && D:\LLVM-LATEST\bin\clang.exe -nostartfiles -nostdlib -O0 -g -Xclang -gcodeview -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -Xlinker /subsystem:console  -fuse-ld=lld-link CMakeFiles/cmTC_bc617.dir/testCCompiler.c.obj -o cmTC_bc617.exe -Xlinker /MANIFEST:EMBED -Xlinker /implib:cmTC_bc617.lib -Xlinker /pdb:cmTC_bc617.pdb -Xlinker /version:0.0   -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames  && cd ."
    lld-link: error: could not open 'kernel32.lib': no such file or directory
    lld-link: error: could not open 'user32.lib': no such file or directory
    lld-link: error: could not open 'gdi32.lib': no such file or directory
    lld-link: error: could not open 'winspool.lib': no such file or directory
    lld-link: error: could not open 'shell32.lib': no such file or directory
    lld-link: error: could not open 'ole32.lib': no such file or directory
    lld-link: error: could not open 'oleaut32.lib': no such file or directory
    lld-link: error: could not open 'uuid.lib': no such file or directory
    lld-link: error: could not open 'comdlg32.lib': no such file or directory
    lld-link: error: could not open 'advapi32.lib': no such file or directory
    lld-link: error: could not open 'oldnames.lib': no such file or directory
    lld-link: error: could not open 'msvcrtd.lib': no such file or directory
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!

It seems that the correct lib path is not passed to the linker, so, I add some clang option to the CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(myproj C)
add_compile_options(
-Xmicrosoft-visualc-tools-root
"D:\EWDK\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.31.31103"
-Xmicrosoft-windows-sdk-root
"D:\EWDK\Program Files\Windows Kits\10")
add_executable(test test.c)

still does not work. So my question is how to influence cmake to pass the correct options when detecting compiler ? Many thanks!

CMake assumes that your programming environment is correctly setup. On windows that usually means running vcvars.bat or some similar compiler setup script. The compiler should work from the command line that you are running CMake from.

Finally , I found the solution

  1. The CMakeLists.txt
cmake_minimum_required(VERSION 3.29)
project(myproj C)
add_executable(test test.c)
  1. create the clang config file (d:\llvm-latest\clang.cfg), the content is as follow
-Xmicrosoft-visualc-tools-root "D:/EWDK/Program Files/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.31.31103"
-Xmicrosoft-windows-sdk-root "D:/EWDK/Program Files/Windows Kits/10"
  1. mkdir build && cd build
  2. in build directory
cmake .. -G Ninja -DCMAKE_EXE_LINKER_FLAGS="--config d:/llvm-latest/clang.cfg" -DCMAKE_C_FLAGS_RELEASE="--config d:/llvm-latest/clang.cfg" -DCMAKE_BUILD_TYPE=Release

There is a simpler way : in step 4

cmake .. -G Ninja -DCMAKE_C_FLAGS="--config d:/llvm-latest/clang.cfg"

Thank you guys.