In CMake, why can't if(CMAKE_BUILD_TYPE STREQUAL "Debug") select Debug-only definitions under a multi-config generator, and what replaces it?
answer
- one configure run, many configurations
- CMAKE_BUILD_TYPE is a single-config variable
- a condition per configuration
- $<CONFIG:...> yields 1 or 0
basics
~10 sMulti-config generators such as Visual Studio, Xcode and Ninja Multi-Config generate every configuration from one configure run, so an if() runs once and cannot differ per configuration. $<$CONFIG:Debug:IMGPROC_CHECKS> is evaluated separately for each configuration.
solid answer
~40 s`if()` executes once, at configure time. A single-config generator (Ninja, Unix Makefiles) builds one configuration chosen by `CMAKE_BUILD_TYPE`, so the test happens to work there. A multi-config generator (Visual Studio, Xcode, Ninja Multi-Config) writes rules for all configurations in `CMAKE_CONFIGURATION_TYPES` from that one run, and the configuration is chosen later with `cmake --build build --config Release`. `CMAKE_BUILD_TYPE` is then not what the build uses: in a Ninja Multi-Config run with `-DCMAKE_BUILD_TYPE=Debug`, `if()` saw `Debug` while Debug, Release and RelWithDebInfo were all generated, so a branch on it would apply to all of them. Write `target_compile_definitions(imgproc PRIVATE $<$<CONFIG:Debug>:IMGPROC_CHECKS>)`, which is evaluated per configuration and works with either generator kind. `if($<CONFIG:Debug>)` does not help: `if()` never evaluates generator expressions.
code
cmake · 14 linesadd_library(imgproc STATIC blur.cpp)
# Wrong under multi-config generators: runs once, at configure time
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(imgproc PRIVATE IMGPROC_CHECKS)
endif()
# Right everywhere: evaluated per configuration at generate time
target_compile_definitions(imgproc PRIVATE $<$<CONFIG:Debug>:IMGPROC_CHECKS>)
# Never true: if() does not evaluate generator expressions
if($<CONFIG:Debug>)
message(STATUS "unreachable")
endif()go deeper
Recall that if() runs once while CMakeLists.txt is processed, and that $<$CONFIG:Debug:...> is the per-configuration way to add a setting.
Explain single- versus multi-config generators, why CMAKE_BUILD_TYPE means nothing to the latter, and why if() never evaluates a generator expression.
Spot configure-time branches on CMAKE_BUILD_TYPE that modify targets and rewrite them as generator expressions that hold for every generator a team uses.
Insist that build logic is correct for both generator families, because the developer on an IDE generator and the CI on Ninja must get the same build.
## Single-config and multi-config generators CMake's **generators** fall into two families: | Family | Examples | How the configuration is chosen | |---|---|---| | Single-config | Ninja, Unix Makefiles | at configure time, by `CMAKE_BUILD_TYPE`; one build tree per configuration | | Multi-config | Visual Studio, Xcode, Ninja Multi-Config | at build time, by `cmake --build build --config <cfg>`; one build tree holds every configuration in `CMAKE_CONFIGURATION_TYPES` | `CMAKE_BUILD_TYPE` is documented as specifying the build type on single-configuration generators; multi-config generators use `CMAKE_CONFIGURATION_TYPES` instead. ## Why if() cannot follow the configuration `if()` is configure-time logic. It runs **once** per configure, and whatever branch it takes is recorded for every configuration that is later generated. Under a multi-config generator there is no single configuration to compare against: `CMAKE_BUILD_TYPE` is usually empty, or holds whatever a user passed with `-D`, and has no bearing on what `--config` builds. A run that shows the trap, with Ninja Multi-Config and `-DCMAKE_BUILD_TYPE=Debug` passed by habit: - `if(CMAKE_BUILD_TYPE STREQUAL "Debug")` took the Debug branch and printed `if() saw Debug`; - CMake still generated Debug, Release and RelWithDebInfo build files; - anything added inside that branch would have applied to all three. The project looks correct on a developer's single-config Ninja build and is quietly wrong on the Visual Studio or Xcode build another developer uses. ## The generator-expression replacement A **generator expression** is evaluated during generation, separately for each configuration: ```cmake target_compile_definitions(imgproc PRIVATE $<$<CONFIG:Debug>:IMGPROC_CHECKS>) ``` In the same run, `IMGPROC_CHECKS` appeared only in the Debug build file and in neither the Release nor the RelWithDebInfo one. Useful details: - `$<CONFIG:cfgs>` yields `1` if the configuration matches any entry of a comma-separated list, compared case-insensitively. Lists such as `$<CONFIG:Debug,RelWithDebInfo>` need CMake 3.19 or newer. - `$<IF:$<CONFIG:Debug>,dbg,opt>` picks between two values; since CMake 3.28 it short-circuits, so the unused branch is not evaluated. - `$<CONFIG>` alone gives the configuration name, handy in output paths. The same expression also works under single-config generators, where it simply sees the one configuration, so one spelling covers both families. ## Why if($<CONFIG:Debug>) is not a shortcut `if()` does not evaluate generator expressions. It receives the literal text `$<CONFIG:Debug>`, treats it as an ordinary string that does not name a defined variable, and evaluates it as false. In a test run, that `if()` printed `FALSE`. Any code that mixes the two ideas has this bug. ## What still belongs in configure-time logic Not everything should become a generator expression: 1. **Decisions that do not depend on the configuration**, such as an `option()` that enables a feature or a check of the platform, stay in `if()`. 2. **Defaults for `CMAKE_BUILD_TYPE`** on single-config generators remain configure-time code, and the material on build configurations covers how to set them safely. 3. **Per-configuration settings** of targets, such as definitions, options, link items or include paths, go in generator expressions on the target. A practical review rule: any `if()` that reads `CMAKE_BUILD_TYPE` and then modifies a target is a multi-config bug waiting for its first Visual Studio or Xcode user. ## Converting an existing project Projects written for one single-config generator often carry many such branches. A safe conversion: 1. **Find them.** Search for `CMAKE_BUILD_TYPE` in every `CMakeLists.txt` and included `.cmake` file; each hit inside a branch that calls a `target_*` command, or appends to a flags variable, is a candidate. 2. **Move the setting onto the target.** Replace the branch with the same command and a `$<$<CONFIG:...>:...>` item, so the condition travels with the value. 3. **Configure with both kinds of generator.** Generate once with Ninja and once with Ninja Multi-Config, then compare the compile commands for Debug and Release; a difference between the two trees for the same configuration is a branch you missed. 4. **Keep configure-time checks configure-time.** A branch that chooses an optional dependency or a platform feature stays in `if()`; only configuration-dependent target settings move.
- Is $<CONFIG:debug> different from $<CONFIG:Debug>?No. The CMake manual defines the `$<CONFIG:cfgs>` comparison as case-insensitive, so both match a configuration named `Debug`. Since CMake 3.19 `cfgs` may also be a comma-separated list, for example `$<CONFIG:Debug,RelWithDebInfo>`.
- How do you pick a whole value per configuration rather than adding or omitting one item?Use `$<IF:condition,true_value,false_value>`, for example `$<IF:$<CONFIG:Debug>,imgproc_d,imgproc>` for an output name. Since CMake 3.28 the unused branch is not evaluated, which matters when that branch contains an expression that would be invalid in the current context.
- CMAKE_BUILD_TYPE selects the configuration in Visual Studio builds
- if($<CONFIG:Debug>) tests the configuration at build time
- Generator expressions only work with multi-config generators
- $<CONFIG:Debug> is case-sensitive, so debug never matches