I’m newly transitioning to Visual Studio as a development environment after many years exclusively using the command line only. I have to work with CMake projects; I will use Google Test 1.14.0 as an example for this question.
My understanding is that to work with “CMake projects” in Visual Studio, you’re supposed to choose the Open a local folder
option on a folder that contains a CMakeLists.txt
.
This seems to do something intelligent: I see my project files/directories laid out in the Solution Explorer pane.
I’m trying to get acclimated to how the IDE interacts with CMake, how it invokes it, how it specifies -D
flags, etc. I noticed that the CMake Settings
window has multiple ways to specify some CMake cache variables, e.g. CMAKE_INSTALL_PREFIX
. In the screenshot below, note that CMAKE_INSTALL_PREFIX
is specifiable in the “Cmake variables and cache” section as well as in the “Advanced > Install directory” text box:
Accidentally, I had different values at the two different fields, and this seems to have the effect of adding two different instances of -DCMAKE_INSTALL_PREFIX
to the command-line invocation of cmake.exe
:
1> Command line: "C:WINDOWSsystem32cmd.exe" /c "%SYSTEMROOT%System32chcp.com 65001 >NUL && "c:program filesmicrosoft visual studio2022enterprisecommon7idecommonextensionsmicrosoftcmakeCMakebincmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:devcmake_udemy_effective_modern_cmake_googletestbuildinstallx64-Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:/dev/cmake_udemy_effective_modern_cmake/_googletest/out/install/x64-Debug" -DCMAKE_MAKE_PROGRAM="c:program filesmicrosoft visual studio2022enterprisecommon7idecommonextensionsmicrosoftcmakeNinjaninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/projects/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:devcmake_udemy_effective_modern_cmake_googletest" 2>&1"
Apparently, Visual Studio aggregates these configuration choices in a CMakeSettings.json
, and I found that indeed, in that file, there were two key/value pairs that corresponded to the install-prefix configuration:
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\out\build${name}",
"installRoot": "${projectDir}\build\install${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"variables": [
{
"name": "CMAKE_INSTALL_PREFIX",
"value": "C:/dev/cmake_udemy_effective_modern_cmake/_googletest/out/install/x64-Debug",
"type": "PATH"
}
]
}
]
}
My question is: is there any way to configure Visual Studio to avoid conflicting values for CMake cache variables?
This gives me concern that there are other places where configurations are settable at multiple locations in the UI; or perhaps there’s a third or fourth way to set CMAKE_INSTALL_PREFIX
hidden somewhere in the UI waiting for me inadvertently set and end up with weird configurations.
(I guess that the IDE is trying to intelligently represent many configurations into some manner of unified UI, perhaps with “human-facing” abstractions over the raw CMake cache variables, but offering multiple ways to set the same setting seems undesirable: a source of bugs, etc.)