Specifying Toolset with Ninja Generator in Visual Studio vs Vanilla CMake

Hi all!

Given the following CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(MyProj LANGUAGES CXX)
add_executable(MyProj src.cpp)

and CMakePresets.json

{
    "version": 6,
    "configurePresets": [
      {
        "name": "Config",
        "binaryDir": "build",
        "generator": "Ninja Multi-Config",
        "toolset":{
          "value": "host=x64, version=14.37"
        },
        "cacheVariables": {
            "CMAKE_CXX_COMPILER": "cl"
        }
      }
    ]
}

I can open the folder that contains these files in Visual Studio 2022 and it will configure without issue using the Visual Studio CMake integration. However if I execute the following command:
cmake --preset Config I get the following error:

Preset CMake variables:

  CMAKE_CXX_COMPILER="cl"

CMake Error at CMakeLists.txt:3 (project):
  Generator

    Ninja Multi-Config

  does not support toolset specification, but toolset

    host=x64, version=14.37

  was specified.


-- Configuring incomplete, errors occurred!

After doing some research I found a few reasons why which is all quite nicely laid out in this post. My questions from this would be:

  1. Why is the Visual Studio CMake integration able to work with this preset and configure the project in the way I want? i.e. Ninja generator while using a specific version of the MSVC compiler.
  2. Is there a way for me to execute cmake --preset Config and achieve the same behaviour? Ideally without having to run vcvarsall.

Try WindowsToochain

1 Like

Sorry for the late reply. This is indeed the type of thing that I was looking for. I wish it was easier to set up the same environment that VS has. Making it so that using the CMake integration in VS was the same experience as using the command line would be great.