Error when I open CMake as a new project in Visual Studio

When I open CMake in Visual Studio 2022 , I got this error:

CMake Error at C: /VisualStudio/Common7 / IDE / CommonExtensions / Microsoft / CMake / CMake / share / cmake - 3.25 / Modules / CMakeTestCCompiler.cmake: 70(message): The C compiler "C:/VisualStudio/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe"
is not able to compile a simple test program.
It fails with the following output: Change Dir: C: /VisualStudio/Common7 / IDE / CommonExtensions / Microsoft / CMake / CMake / bin / CMakeProject1 / out / build / x64 - debug / CMakeFiles / CMakeScratch / TryCompile - ytu82w
Run Build Command(s): C: \VISUALSTUDIO\ COMMON7\ IDE\ COMMONEXTENSIONS\ Microsoft\ CMake\ Ninja\ ninja.exe cmTC_d465b && [1 / 2] Building C object CMakeFiles\ cmTC_d465b.dir\ testCCompiler.c.obj[2 / 2] Linking C executable cmTC_d465b.exe
FAILED: cmTC_d465b.exe
cmd.exe / C "cd . && C:\VisualStudio\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_d465b.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests  -- C:\VisualStudio\VC\Tools\MSVC\14.35.32215\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_d465b.dir\testCCompiler.c.obj  /out:cmTC_d465b.exe /implib:cmTC_d465b.lib /pdb:cmTC_d465b.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 /fo CMakeFiles\cmTC_d465b.dir/manifest.res CMakeFiles\cmTC_d465b.dir/manifest.rc"
failed(exit code 0) with the following output: The system cannot find the file specified
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.

Any help would be appreciated

I get the same problem when I try to use Ninja as my generator, but don’t run vcvarsall.bat and don’t set some other path variables, path to some libraries, etc.
So, what I do is to set the compiler and generator to cl.exe and Visual Studio 17 2022, respectively. This solves all the problems.

And since I, as I said on my other reply, have been using MSVC only, I might as well use MS’s Generator :man_shrugging:t5:
Unless I am not working with C++20 Modules and just want to try something real quick, then I use Ninja(the build directory is cleaner | the EXPORT_COMPILE_COMMANDS would work :joy:)
However, I HAVE been using Modules all the time, so …

You may be missing the command line tools and/or the SDK in the Visual Studio component selection.

They are all installed . I set the path to Windows kits SDK in windows environment variables like this :

C:\Program Files (x86)\Windows Kits\8.1\bin\x64 but still did not work

Hmm. Did you install the “(Latest)” build tools? I feel like there’s one time I saw that as needed (even if you’re using a versioned toolchain).