Best Practice for Ninja build Visual Studio?


What is the recommended way to configure/invoke CMake such that it will use Ninja as the build tool but still locate the compiler/etc the way it would if you had specified the Visual Studio Code generator to use msbuild?

This isn’t just about forgetting to start a developer command prompt/pwsh, but it’s very common from desktop applications using the system/user paths, such as VS Code:

[cmake] CMake Error at CMakeLists.txt:14 (PROJECT):
[cmake]   No CMAKE_CXX_COMPILER could be found.

To reproduce:

  1. Open a prompt and confirm it does not have the vsdev environment variables loaded, and you cannot run cl.exe
  2. Ensure that ninja is in your path (e.g nuget/choco install it, or copy your vsdev ninja into c:\windows or something)
  3. Ensure cmake is in your path
  4. Create a trivial Hello World project
  5. cmake -G Ninja . build

The Ninja (really, all non-IDE generators) expect the environment to have the compilers set up already. You’ll need to load the compiler environment before you can use such generators. Granted, this is “easy” on Unix-like platforms since the compiler is usually set up system-wide, but this is not the case for the Visual Studio toolchains (among other compilers in various places and systems which use module load to make toolchains available).

I have the right environment but get this:

C:\Users\klein_cl\Workspace\cpp\minijson_reader>cmake -G Ninja -B build-ninja --check-system-vars --debug-trycompile
Also check system files when warning about unused and uninitialized variables.
debug trycompile on
-- The CXX compiler identification is MSVC 19.28.29914.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/CMakeTestCXXCompiler.cmake:62 (message):
  The C++ compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/klein_cl/Workspace/cpp/minijson_reader/build-ninja/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/RsPython/scripts/ninja.exe cmTC_c0149 && [1/2] Building CXX object CMakeFiles\cmTC_c0149.dir\testCXXCompiler.cxx.obj
    [2/2] Linking CXX executable cmTC_c0149.exe
    FAILED: cmTC_c0149.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_c0149.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests  -- C:\PROGRA~2\MIB055~1\2019\PROFES~1\VC\Tools\MSVC\1428~1.299\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_c0149.dir\testCXXCompiler.cxx.obj  /out:cmTC_c0149.exe /implib:cmTC_c0149.lib /pdb:cmTC_c0149.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_c0149.dir/manifest.res CMakeFiles\cmTC_c0149.dir/manifest.rc" failed (exit code 0) with the following output:
    Das System kann die angegebene Datei nicht finden
    ninja: build stopped: subcommand failed.

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

-- Configuring incomplete, errors occurred!
See also "C:/Users/klein_cl/Workspace/cpp/minijson_reader/build-ninja/CMakeFiles/CMakeOutput.log".
See also "C:/Users/klein_cl/Workspace/cpp/minijson_reader/build-ninja/CMakeFiles/CMakeError.log".


I need to execute vcvars, than it works!

call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"

You should also have a maybe more convenient direct command prompt shortcut (search for a native tools shortcut in the start menu) that calls vcvars for you, in my case it calls:

C:\Windows\System32\cmd.exe /k "C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat"