# How to set Release mode directly in CMakeLists.txt / CMakePresets.json? "Visual Studio 18 2026", vcpkg, case example with library "google/benchmark"

**URL:** https://discourse.cmake.org/t/how-to-set-release-mode-directly-in-cmakelists-txt-cmakepresets-json-visual-studio-18-2026-vcpkg-case-example-with-library-google-benchmark/15635
**Category:** Code
**Created:** [April 28, 2026, 1:01am UTC](https://discourse.cmake.org/t/how-to-set-release-mode-directly-in-cmakelists-txt-cmakepresets-json-visual-studio-18-2026-vcpkg-case-example-with-library-google-benchmark/15635 "2026-04-28T01:01:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![codev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/53a042/32.png) [@codev](https://discourse.cmake.org/u/codev)
#### Post date: [April 28, 2026, 1:01am UTC](https://discourse.cmake.org/t/how-to-set-release-mode-directly-in-cmakelists-txt-cmakepresets-json-visual-studio-18-2026-vcpkg-case-example-with-library-google-benchmark/15635/1 "2026-04-28T01:01:34Z")

</div>

What would be the way to specify the compilation in `Release` mode when working with CMake, ideally in “CMakeLists.txt”, but if not then in “CMakePresets.json”.

Looking for a solution mainly without commands because this question is about when working with VSCode, launching the compilation with the Launch/Build buttons at the bottom editor window frame. So, a “command solution” only would be a complement if needed for other editors. The goal is to be able to specify the setting directly in CMakeLists.txt or CMakePresets.json.

I’ve read about this in multiple threads but none of the options have worked, either showing compilation errors, or compiling in `Debug` mode and only generating the path “build \> Debug”.

  
Extra context:
- Compilation on Windows with “Visual Studio 18 2026”, for the moment, however the ideal solution is a cross-platform way to specify `Release` compilation mode (and also other modes like `Debug` mode, when needed).

- Tests shown in the files below have not worked with a simple “Hello World!” program with just a “main.cpp”. However, the idea is to extend the solution when working with a package manager like vcpkg.

- A practical case example would be when working with the library “google/benchmark” downloaded with vcpkg, which, for reference, is compiling but in `Debug` mode as the rest of the other programs, so for that reason showing the message:  
“_ **WARNING** _ Library was built as DEBUG. Timings may be affected.”

  

CMakeLists.txt

```auto
cmake_minimum_required(VERSION 3.30)

project(project_A VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# set(CMAKE_BUILD_TYPE Release)
# set(CMAKE_BUILD_TYPE release)
# set(cacheVariables.CMAKE_BUILD_TYPE Release)
# set(CMAKE_BUILD_TYPE "Release")
# set(CMAKE_BUILD_TYPE Debug)
# SET(CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING "" FORCE)
# set(CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING "Build types" FORCE)
# set(CMAKE_CONFIGURATION_TYPES Release)
# set(CMAKE_CONFIGURATION_TYPES "Release")
# set(CMAKE_DEFAULT_BUILD_TYPE Release)

# if(MSVC)
# # set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2 /DNDEBUG")
# # if (BENCHMARK_ENABLE_LTO)
# set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
# set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
# set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
# set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
# # endif()
# endif()

# find_package(benchmark REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

# target_compile_options(${PROJECT_NAME} PRIVATE
# $<$<CONFIG:Release>:/O2 /DNDEBUG> # MSVC Release flags
# #$<$<CONFIG:Debug>:/Od /Zi> # MSVC Debug flags (optional)
# )

# target_compile_definitions(${PROJECT_NAME} PRIVATE RELEASE_BUILD)

# target_link_libraries(${PROJECT_NAME} PRIVATE benchmark::benchmark)

```

  

CMakePresets.json

```auto
{
  "version": 9,
  "configurePresets": [
    {
      "name": "VS and vcpkg",
      "displayName": "VS and vcpkg",
      "generator": "Visual Studio 18 2026",
      "architecture": "x64",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": ".../vcpkg.cmake",
        "VCPKG_TARGET_TRIPLET": "x64-windows"
      }
    }
  ]
}

```

Also trying with the next in “CMakePresets.json”:

```auto
...
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": ".../vcpkg.cmake",
        "VCPKG_TARGET_TRIPLET": "x64-windows",
        "CMAKE_BUILD_TYPE": "Release",
        "CMAKE_CONFIGURATION_TYPES": "Release"
      }
...

```

  
Some threads about this topic already visited:

> <https://stackoverflow.com/questions/7724569/debug-vs-release-in-cmake>

> <https://stackoverflow.com/questions/19024259/how-to-change-the-build-type-to-release-mode-in-cmake>

and other threads…

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [April 28, 2026, 8:49am UTC](https://discourse.cmake.org/t/how-to-set-release-mode-directly-in-cmakelists-txt-cmakepresets-json-visual-studio-18-2026-vcpkg-case-example-with-library-google-benchmark/15635/2 "2026-04-28T08:49:42Z")

</div>

> way to specify the compilation in `Release` mode when working with CMake […] in `CMakePresets.json`

The `CMAKE_BUILD_TYPE` in `cacheVariables` inside `CMakePresets.json` is the way, and you already have that, and that would have worked for Ninja generator, but Visual Studio generators do not(?) care about that variable - the Debug/Release type of build is specified with `--config Debug` (_or `--config Release`_) when running `cmake --build`, so after the project has already been configured. Setting `CMAKE_BUILD_TYPE` in `CMakeLists.txt` instead of `CMakePresets.json` wouldn’t make a difference either (_for Visual Studio generators_).

So in addition to the configuration preset, for Visual Studio generators you will also need to add a [build preset](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#build-preset), which can specify `configuration` to be `Release`. And while at it, you most likely will also want to pass the `/m` option in `nativeToolOptions`, as MSBuild does not(?) parallelize building by default. So, everything together:

```json
{
    "name": "some-windows-vs-build",
    "configurePreset": "some-windows-vs-configuration",
    "configuration": "Release",
    "targets": "install",
    "nativeToolOptions":
    [
        "/m"
    ]
}

```

The `"targets": "install"` is here because I am assuming that you have a proper installation procedure in your project.

> library […] downloaded with vcpkg […] is compiling but in `Debug` mode

Dependencies/packages that are resolved with vcpkg are built in both Debug and Release configurations (_so they are built twice_), regardless of whichever build type you’ve set for your project. Well, unless you have modified the triplet and restricted the build type to just the Debug (_or just the Release_).

> Looking for a solution mainly without commands because this question is about when working with VSCode, launching the compilation with the Launch/Build buttons

From what I know about VS Code, you can compose [custom tasks](https://code.visualstudio.com/docs/debugtest/tasks#_custom-tasks), which can be CMake calls with whatever commands you might want. And those tasks likely can be tied to buttons, or at the very least they will be callable from the Command Palette.

---

<div class="post-metadata">

### Author: ![LegalizeAdulthood](https://discourse.cmake.org/user_avatar/discourse.cmake.org/legalizeadulthood/32/5282_2.png) [@LegalizeAdulthood](https://discourse.cmake.org/u/LegalizeAdulthood)
#### Post date: [April 28, 2026, 10:04pm UTC](https://discourse.cmake.org/t/how-to-set-release-mode-directly-in-cmakelists-txt-cmakepresets-json-visual-studio-18-2026-vcpkg-case-example-with-library-google-benchmark/15635/3 "2026-04-28T22:04:22Z")

</div>

It’s not so much Visual Studio, as any generator that is [“multi-config”](https://cmake.org/cmake/help/latest/prop_gbl/GENERATOR_IS_MULTI_CONFIG.html). This applies to multiple generators on multiple platforms and not just Visual Studio.
