Visual Studio 2017 ignores custom CMake executable set in CMakeSettings.json via cmakeExecutable property

Viewed 458

I want to use a more current version of CMake in my CMake project. Currently Visual Studio 2017 is using

c:\program files (x86)\microsoft visual studio\2017\professional\common7\ide\commonextensions\microsoft\cmake\CMake\bin\cmake.exe

which is 3.12.

I have an installation of CMake 3.21 in c:\program files\CMake\bin. According to the official documentation I can use cmakeExecutable inside the CMakeSettings.json configuration:

{
  "configurations": [
    {
      "name": "x64-Debug",
      "cmakeExecutable": "C:\\Program Files\\CMake\\bin\\cmake.exe",
      "cmakeCommandArgs": "-j4",
      "generator": "Visual Studio 15 2017",
      "configurationType": "Debug",
      "inheritEnvironments": [
        "msvc_x64_x64"
      ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": ""
    }
  ]
}

Whenever I try to configure my project with

cmake_minimum_required(VERSION X)

with X being anything above 3.12 I get

CMake Error at feld/CMakeLists.txt:1 (cmake_minimum_required): CMake 3.20 or higher is required. You are running version 3.12.18081601-MSVC_2

I can also clearly see the call that VS does in the output tab right after I press the generation of the cache:

1> Command line: c:\program files (x86)\microsoft visual studio\2017\professional\common7\ide\commonextensions\microsoft\cmake\CMake\bin\cmake.exe -G "Visual Studio 15 2017" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\user\CMakeBuilds\ef5b5ada-ee42-7735-988a-ae37c735ccff\install\x64-Debug" -DCMAKE_CXX_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe" -DCMAKE_C_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe" -j4 -DCMAKE_CONFIGURATION_TYPES="Debug" "C:\Users\user\Documents\myproject"

To me this appears to be a bug. However I lack the experience on Windows and with Visual Studio so there is hopefully something I am missing. I can replace the CMake in that directory by simply copying my current installation over but this requires admin privileges, which should not be a requirement for configuring your project.


UPDATE and WARNING: Replacing the CMake that is shipped with VS by simply overwriting the files (binaries etc.) is a bad idea. I had to repair my VS installation after attempting to do so since even uninstalling and reinstalling the CMake components using the VS Installer doesn't fix the broken mess that resulted in that experiment. In addition even putting a new module in VS CMake leads to issues. I tried to put FindCUDAToolkit.cmake (which is one of the main reasons why I need a more recent CMake version since it comes shipped with it) and it resulted in a bizarre error message from VS CMake that target_link_libraries(...) is an unknown CMake command.

If the issue with the configuration I have described in this post is indeed a bug you will have to adapt your workflow. Right now I'm using an up-to-date CMake 3.21 outside of VS (command line or CMake GUI), disabled everything related to CMake integration in the IDE (especially automatic cache generation upon change), generate the solution with the external CMake, open it in another instance of VS and build/debug it there. Far from an optimal solution but it works.

0 Answers
Related