# Unexpected behavior when regenerating a project with Ninja Multi-Config and MSVC

**URL:** https://discourse.cmake.org/t/unexpected-behavior-when-regenerating-a-project-with-ninja-multi-config-and-msvc/14099
**Category:** Usage
**Tags:** os:windows, comp:msvc, gen:ninja
**Created:** [May 15, 2025, 4:22am UTC](https://discourse.cmake.org/t/unexpected-behavior-when-regenerating-a-project-with-ninja-multi-config-and-msvc/14099 "2025-05-15T04:22:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![aeart](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/3e96dc/32.png) [@aeart](https://discourse.cmake.org/u/aeart)
#### Post date: [May 15, 2025, 4:22am UTC](https://discourse.cmake.org/t/unexpected-behavior-when-regenerating-a-project-with-ninja-multi-config-and-msvc/14099/1 "2025-05-15T04:22:53Z")

</div>

I have encountered an unusual behavior when regenerating a project using **Ninja Multi-Config** as the generator and **Microsoft Visual C++ (MSVC)** as the compiler.

Here is a minimal example project consisting of three files:

#### 1. `CMakeLists.txt`

```cmake
cmake_minimum_required(VERSION 3.15)

PROJECT(main)

find_package(Eigen3 CONFIG REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} Eigen3::Eigen)

```

#### 2. `CMakePresets.json`

```json
{
  "version": 10,
  "cmakeMinimumRequired": {
    "major": 4,
    "minor": 0
  },
  "configurePresets": [
    {
      "name": "default",
      "generator": "Ninja Multi-Config",
      "binaryDir": "${sourceDir}/build",
      "environment": {
        "VCPKG_HOST_TRIPLET": "x64-windows",
        "VCPKG_TARGET_TRIPLET": "x64-windows",
        "VCPKG_INSTALLED_DIR": "$env{VCPKG_ROOT}/installed"
      },
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
        "CMAKE_C_COMPILER": "cl",
        "CMAKE_CXX_COMPILER": "cl"
      }
    }
  ]
}

```

#### 3. `main.cpp`

```cpp
#include <iostream>

int main()
{
  std::cout << "Hello\n";
  return 0;
}

```

* * *

When I run the following command for the **first time** :

```bash
cmake --preset=default

```

I get the expected output:

```auto
-- The C compiler identification is MSVC 19.43.34810.0
-- The CXX compiler identification is MSVC 19.43.34810.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.8s)
-- Generating done (0.0s)
-- Build files have been written to: F:/test_projects/test_cmake_ninja_multi/build

```

The project is successfully generated.

However, on **the second invocation** of the same command:

```bash
cmake --preset=default

```

I receive the following message:

```auto
-- Configuring done (0.1s)
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= cl
CMAKE_CXX_COMPILER= cl

-- The C compiler identification is MSVC 19.43.34810.0
-- The CXX compiler identification is MSVC 19.43.34810.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:5 (find_package):
  Could not find a package configuration file provided by "Eigen3" with any
  of the following names:

    Eigen3Config.cmake
    eigen3-config.cmake

  Add the installation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set
  "Eigen3_DIR" to a directory containing one of the above files. If "Eigen3"
  provides a separate development package or SDK, be sure it has been
  installed.

-- Configuring incomplete, errors occurred!

```

CMake now fails to find `Eigen3`, which was previously located without issue. Eigen is installed via `vcpkg`.

On the **third invocation** , the same error occurs again:

```auto
CMake Error at CMakeLists.txt:5 (find_package):
  Could not find a package configuration file provided by "Eigen3" with any
  of the following names:

    Eigen3Config.cmake
    eigen3-config.cmake

  Add the installation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set
  "Eigen3_DIR" to a directory containing one of the above files. If "Eigen3"
  provides a separate development package or SDK, be sure it has been
  installed.

-- Configuring incomplete, errors occurred!
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= cl
CMAKE_CXX_COMPILER= cl

-- Generating done (0.0s)
CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_TOOLCHAIN_FILE

CMake Generate step failed. Build files cannot be regenerated correctly.

```

However, on the **fourth invocation** , everything works as expected again — identical to the first run. The project is configured successfully.

* * *

Interestingly, this issue does **not** occur when using the `clang-cl` compiler instead of MSVC.

* * *

Has anyone else experienced this kind of behavior when using Ninja Multi-Config with MSVC and CMake presets? Is this a known issue, or am I misconfiguring something?

Any insights would be appreciated.

---

<div class="post-metadata">

### Author: ![aeart](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/3e96dc/32.png) [@aeart](https://discourse.cmake.org/u/aeart)
#### Post date: [June 19, 2025, 1:37am UTC](https://discourse.cmake.org/t/unexpected-behavior-when-regenerating-a-project-with-ninja-multi-config-and-msvc/14099/2 "2025-06-19T01:37:59Z")

</div>

**Update:** I didn’t receive a response here, so I created an [issue on GitLab](https://gitlab.kitware.com/cmake/cmake/-/issues/27005), where I got an answer.

It turned out that I shouldn’t explicitly specify the compilers like this:

```auto
        "CMAKE_C_COMPILER": "cl",
        "CMAKE_CXX_COMPILER": "cl"

```

Instead, I should use an environment where CMake can detect the compiler automatically.

Hope this helps someone who encounters a similar issue.
