# How to deal with Ninja setting CMAKE\_BUILD\_TYPE to Debug?

**URL:** https://discourse.cmake.org/t/how-to-deal-with-ninja-setting-cmake-build-type-to-debug/281
**Category:** Code
**Created:** [November 28, 2019, 2:11pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ninja-setting-cmake-build-type-to-debug/281 "2019-11-28T14:11:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [November 28, 2019, 2:11pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ninja-setting-cmake-build-type-to-debug/281/1 "2019-11-28T14:11:17Z")

</div>

I am running CMake 3.14.5 on Windows with Ninja and MSVC compiler. I am also building the same project on Linux using Ninja.

As suggested by [CMake and the Default Build Type](https://blog.kitware.com/cmake-and-the-default-build-type/) I set the build type to Release if it has not been set:

```
set(default_build_type "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
  set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
      STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
    "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

```

However, I am finding that Ninja on WIndows sets CMAKE\_BUILD\_TYPE to Debug, even if not set by the user. So the above code does not work. I could force the build type to Release but that would not respect an intentional setting of Debug by the user.

Why is Ninja doing this and how can I get around it?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 28, 2019, 2:30pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ninja-setting-cmake-build-type-to-debug/281/2 "2019-11-28T14:30:01Z")

</div>

You can just set the cached variable `CMAKE_BUILD_TYPE` **without** the keyword `FORCE`. So, it will set **only** if not already defined (through command line `-DCMAKE_BUILD_TYPE=...` for example).

Moreover, to check if the configuration is multi-config, the preferred way is to pick-up global property [GENERATOR\_IS\_MULTI\_CONFIG](https://cmake.org/cmake/help/latest/prop_gbl/GENERATOR_IS_MULTI_CONFIG.html) value rather than using variable `CMAKE_CONFIGURATION_TYPES`:

```cmake
get_property (is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)

```

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [November 28, 2019, 2:40pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ninja-setting-cmake-build-type-to-debug/281/3 "2019-11-28T14:40:25Z")

</div>

@marc.chevrier Thanks for your reply and suggestions.

> You can just set the cached variable `CMAKE_BUILD_TYPE` **without** the keyword `FORCE` . So, it will set **only** if not already defined …

My point is that I think Ninja **is setting** CMAKE\_BUILD\_TYPE to Debug, i.e. CMAKE\_BUILD\_TYPE is set before my CMake code is reached so my default of Release is not being used. I don’t know why Ninja is doing this as I believe it is not a multi-configuration generator. It seems to only happen on Windows.

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 28, 2019, 3:08pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ninja-setting-cmake-build-type-to-debug/281/4 "2019-11-28T15:08:10Z")

</div>

The definition of `CMAKE_BUILD_TYPE` must occurred **before** the `project` command:

```cmake
set (CMAKE_BUILD_TYPE "Release" CACHE
      STRING "Choose the type of build.")

project (MyProject ...)

```

Using this snippet, you manage a default value and take care of user’s choice done through command line.

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [November 28, 2019, 3:20pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ninja-setting-cmake-build-type-to-debug/281/5 "2019-11-28T15:20:55Z")

</div>

@marc.chevrier Thank you. That the definition of `CMAKE_BUILD_TYPE` must occur **before** the `project` command, fixed the problem for me.
