# Figure out the build type on Windows

**URL:** <https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295>\
**Category:** Code\
**Tags:** os:linux, os:windows, os:macos\
**Created:** [March 25, 2022, 2:48pm UTC](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295 "2022-03-25T14:48:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hungptit](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hungptit/32/2296_2.png) [@hungptit](https://discourse.cmake.org/u/hungptit)\
**Post date:** [March 25, 2022, 2:48pm UTC](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295/1 "2022-03-25T14:48:52Z")

</div>

I assume this question has been asked somewhere on the Web, but I could not find anything usable for my use cases. What is the proper way to handle this logic on all platforms?

```auto
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  set(STEM "Debug")
else()
  set(STEM "Release")
endif()

```

---

<div class="post-metadata">

**Author:** ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)\
**Post date:** [March 25, 2022, 5:08pm UTC](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295/2 "2022-03-25T17:08:17Z")

</div>

You shouldn’t use CMAKE\_BUILD\_TYPE. You should use [generator expressions](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#genex:CONFIG).

As an example:

```auto
# Compile foobar library with per-preprocessor macro DEBUG_MODE only on debug builds
add_library(foobar STATIC)
target_compile_definitions(foobar PRIVATE $<$<CONFIG:Debug>:DEBUG_MODE>)

```

Here is a more in [detail stackoverflow post](https://stackoverflow.com/a/64719718)

---

<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:** [March 27, 2022, 11:18am UTC](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295/3 "2022-03-27T11:18:59Z")

</div>

In fact, it depends on the generator.  
For multi-config generators (`Visual Studio` for instance), `CMAKE_BUILD_TYPE` cannot be used, so only genex are usable.

For mono-config generators (`NMake Makefiles` for instance), CMAKE\_BUILD\_TYPE` is usable but genex are also usable.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [March 29, 2022, 1:21pm UTC](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295/4 "2022-03-29T13:21:16Z")

</div>

As stated, yes, the logic you want isn’t possible for any given generator (because the build type in multi-config generators is chosen after all your CMake code has executed).

However, what are you trying to accomplish? If it is to have different names on libraries based on the configuration, the [`<CONFIG>_POSTFIX`](https://cmake.org/cmake/help/latest/prop_tgt/CONFIG_POSTFIX.html) target property is the way to go.

---

<div class="post-metadata">

**Author:** ![hungptit](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hungptit/32/2296_2.png) [@hungptit](https://discourse.cmake.org/u/hungptit)\
**Post date:** [April 6, 2022, 4:30pm UTC](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295/5 "2022-04-06T16:30:26Z")

</div>

Thank you all for your suggestions. I need to set up the search paths based on the build type, and this step belongs to the configuration stage. I have a workaround by passing an extra parameter from the build script. However, having CMake do this work based on the build config is a cleaner solution.

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [April 6, 2022, 4:45pm UTC](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295/6 "2022-04-06T16:45:49Z")

</div>

target\_include\_directories supports generator expressions, so that’s how I would accomplish this.
