Skip to content

Commit 55088ae

Browse files
authored
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.
1 parent dda89e0 commit 55088ae

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

docs/build/cmake-presets-vs.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,33 @@ The official [CMake documentation](https://cmake.org/cmake/help/latest/manual/cm
205205

206206
### Select your compilers
207207

208-
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=<value>` and `-D CMAKE_CXX_COMPILER=<value>` to CMake from the command line. For more information, see [`CMAKE_<LANG>_COMPILER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html#cmake-lang-compiler).
208+
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).
209209

210210
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`.
211211

212212
Build with `cl.exe`:
213213

214214
```json
215+
"environmentVariables": {
216+
"CC": "cl",
217+
"CXX": "cl"
218+
},
215219
"cacheVariables": {
216220
"CMAKE_BUILD_TYPE": "Debug",
217-
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
218-
"CMAKE_C_COMPILER": "cl",
219-
"CMAKE_CXX_COMPILER": "cl"
221+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
220222
},
221223
```
222224

223225
Build with `clang`:
224226

225227
```json
228+
"environmentVariables": {
229+
"CC": "clang-cl",
230+
"CXX": "clang-cl"
231+
},
226232
"cacheVariables": {
227233
"CMAKE_BUILD_TYPE": "Debug",
228234
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
229-
"CMAKE_C_COMPILER": "clang-cl",
230-
"CMAKE_CXX_COMPILER": "clang-cl"
231235
},
232236

233237
"vendor": {
@@ -264,11 +268,13 @@ To reproduce these builds outside Visual Studio, see [Run CMake from the command
264268
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:
265269

266270
```json
271+
"environmentVariables": {
272+
"CC": "gcc-8",
273+
"CXX": "g++-8"
274+
},
267275
"cacheVariables": {
268276
"CMAKE_BUILD_TYPE": "Debug",
269-
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
270-
"CMAKE_C_COMPILER": "gcc-8",
271-
"CMAKE_CXX_COMPILER": "g++-8"
277+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
272278
},
273279
```
274280

0 commit comments

Comments
 (0)