# simple cmake file failed when generate ninja build script

**URL:** https://discourse.cmake.org/t/simple-cmake-file-failed-when-generate-ninja-build-script/10825
**Category:** Usage
**Created:** [May 9, 2024, 2:10am UTC](https://discourse.cmake.org/t/simple-cmake-file-failed-when-generate-ninja-build-script/10825 "2024-05-09T02:10:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![yushang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yushang/32/4574_2.png) [@yushang](https://discourse.cmake.org/u/yushang)
#### Post date: [May 9, 2024, 2:10am UTC](https://discourse.cmake.org/t/simple-cmake-file-failed-when-generate-ninja-build-script/10825/1 "2024-05-09T02:10:27Z")

</div>

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

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

```

in build directory

```auto
cmake .. -G Ninja

```

failed with

```auto
-- 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

```auto
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!

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [May 10, 2024, 2:47pm UTC](https://discourse.cmake.org/t/simple-cmake-file-failed-when-generate-ninja-build-script/10825/2 "2024-05-10T14:47:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![yushang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yushang/32/4574_2.png) [@yushang](https://discourse.cmake.org/u/yushang)
#### Post date: [May 10, 2024, 5:26pm UTC](https://discourse.cmake.org/t/simple-cmake-file-failed-when-generate-ninja-build-script/10825/3 "2024-05-10T17:26:07Z")

</div>

Finally , I found the solution

1. The CMakeLists.txt

```auto
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

```auto
-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

```auto
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

```

---

<div class="post-metadata">

### Author: ![yushang](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yushang/32/4574_2.png) [@yushang](https://discourse.cmake.org/u/yushang)
#### Post date: [May 12, 2024, 3:58am UTC](https://discourse.cmake.org/t/simple-cmake-file-failed-when-generate-ninja-build-script/10825/4 "2024-05-12T03:58:06Z")

</div>

There is a simpler way : in step 4

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

```

Thank you guys.
