From 55088aecfe477b359dd22bc532e49bc0257d05de Mon Sep 17 00:00:00 2001 From: Carsten Rudolph <18394207+crud89@users.noreply.github.com> Date: Tue, 2 Sep 2025 14:53:03 +0200 Subject: [PATCH 1/2] Use environment variables to specify compiler preferences. The cache variables `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER` must only be set during the initial cache creation. Specifying them in a preset will cause them to be reset during subsequent cache configurations, resulting in a cache invalidation, which can break the build. Using the `CC` and `CXX` environment variables to initialize those cache variables is the proper way to avoid this issue. --- docs/build/cmake-presets-vs.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/build/cmake-presets-vs.md b/docs/build/cmake-presets-vs.md index 87d3fd71461..1706bdbbe17 100644 --- a/docs/build/cmake-presets-vs.md +++ b/docs/build/cmake-presets-vs.md @@ -205,29 +205,33 @@ The official [CMake documentation](https://cmake.org/cmake/help/latest/manual/cm ### Select your compilers -You can set C and C++ compilers by using `cacheVariables.CMAKE_C_COMPILER` and `cacheVariables.CMAKE_CXX_COMPILER` in a Configure Preset. It's equivalent to passing `-D CMAKE_C_COMPILER=` and `-D CMAKE_CXX_COMPILER=` to CMake from the command line. For more information, see [`CMAKE__COMPILER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html#cmake-lang-compiler). +You can set C and C++ compilers by using `environmentVariables.CC` and `environmentVariables.CXX` in a Configure Preset. For more information, see [`CC`](https://cmake.org/cmake/help/latest/envvar/CC.html)/[`CXX`](https://cmake.org/cmake/help/latest/envvar/CXX.html). Use the following examples to build with `cl.exe` and `clang-cl.exe` from Visual Studio. The C++ Clang tools for Windows components must be installed for you to build with `clang-cl`. Build with `cl.exe`: ```json +"environmentVariables": { + "CC": "cl", + "CXX": "cl" +}, "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", - "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}", - "CMAKE_C_COMPILER": "cl", - "CMAKE_CXX_COMPILER": "cl" + "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}" }, ``` Build with `clang`: ```json +"environmentVariables": { + "CC": "clang-cl", + "CXX": "clang-cl" +}, "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}", - "CMAKE_C_COMPILER": "clang-cl", - "CMAKE_CXX_COMPILER": "clang-cl" }, "vendor": { @@ -264,11 +268,13 @@ To reproduce these builds outside Visual Studio, see [Run CMake from the command To build on Linux or without the Visual C++ toolset, specify the name of a compiler on your `PATH` instance, or an environment variable that evaluates to the full path of a compiler. Full paths are discouraged so that the file can remain shareable. A preset that builds with GCC version 8 might look like this: ```json +"environmentVariables": { + "CC": "gcc-8", + "CXX": "g++-8" +}, "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", - "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}", - "CMAKE_C_COMPILER": "gcc-8", - "CMAKE_CXX_COMPILER": "g++-8" + "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}" }, ``` From 453dc40afd8602834b44741cdf173a50919bbff6 Mon Sep 17 00:00:00 2001 From: Carsten Rudolph <18394207+crud89@users.noreply.github.com> Date: Tue, 9 Sep 2025 17:42:48 +0200 Subject: [PATCH 2/2] Fix `environment` name. --- docs/build/cmake-presets-vs.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/build/cmake-presets-vs.md b/docs/build/cmake-presets-vs.md index 1706bdbbe17..5d99333e4c8 100644 --- a/docs/build/cmake-presets-vs.md +++ b/docs/build/cmake-presets-vs.md @@ -205,14 +205,14 @@ The official [CMake documentation](https://cmake.org/cmake/help/latest/manual/cm ### Select your compilers -You can set C and C++ compilers by using `environmentVariables.CC` and `environmentVariables.CXX` in a Configure Preset. For more information, see [`CC`](https://cmake.org/cmake/help/latest/envvar/CC.html)/[`CXX`](https://cmake.org/cmake/help/latest/envvar/CXX.html). +You can set C and C++ compilers by using `environment.CC` and `environment.CXX` in a Configure Preset. For more information, see [`CC`](https://cmake.org/cmake/help/latest/envvar/CC.html)/[`CXX`](https://cmake.org/cmake/help/latest/envvar/CXX.html). Use the following examples to build with `cl.exe` and `clang-cl.exe` from Visual Studio. The C++ Clang tools for Windows components must be installed for you to build with `clang-cl`. Build with `cl.exe`: ```json -"environmentVariables": { +"environment": { "CC": "cl", "CXX": "cl" }, @@ -225,7 +225,7 @@ Build with `cl.exe`: Build with `clang`: ```json -"environmentVariables": { +"environment": { "CC": "clang-cl", "CXX": "clang-cl" }, @@ -268,7 +268,7 @@ To reproduce these builds outside Visual Studio, see [Run CMake from the command To build on Linux or without the Visual C++ toolset, specify the name of a compiler on your `PATH` instance, or an environment variable that evaluates to the full path of a compiler. Full paths are discouraged so that the file can remain shareable. A preset that builds with GCC version 8 might look like this: ```json -"environmentVariables": { +"environment": { "CC": "gcc-8", "CXX": "g++-8" },