3

I know that if if we set -DCMAKE_BUILD_TYPE=Release (or Debug etc.), then the values of CMAKE_C_FLAGS_RELEASE and CMAKE_CXX_FLAGS_RELEASE will be appended to CMAKE_C_FLAGS and CMAKE_C_FLAGS respectively.

But is this the only effect of setting the build type? If not, what are the other effects?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    Just as a quick note: You should avoid the CMake wiki, if possible, as it is fairly outdated and often not very accurate. The [official documentation](https://cmake.org/cmake/help/v3.9/) is a way better source of information (at least for CMake versions 3.0 and up). – ComicSansMS Oct 19 '17 at 11:05

1 Answers1

3

Actually, build type affects on many things. Among them:

  • generator expressions:

    Expression $<$<CONFIG:DEBUG>:XXX> will be expanded to XXX with CMAKE_BUILD_TYPE set to Debug and to nothing otherwise.

    Because generator expressions can be used in a number of commands, setting build type affects all commands which uses expressions dependent on build type.

  • libraries added by target_link_libraries with debug keyword take an effect only in Debug build type.

    Similar to optimized keyword.

    (Implicitely, this uses generator expressions described above).

  • Some properies of IMPORTED libraries.

    Properties like IMPORTED_LOCATION have config-specific variants, which are choosen dependent on configuration type.

    Often IMPORTED libraries are created as a result of find_package() call, so your project may be linked with 3d-party project in configuration-dependent manner.

  • CONFIGURATION-specific part of install command.

    Only those CONFIGURATION <conf> part are applies, which corresponds to active configuration.


Multi-configuration tools doesn't use CMAKE_BUILD_TYPE variable, but they still have a notion of the "build type". That build type is NOT known at configuration stage, when CMake parses CMakeLists.txt, it is set only when performing a build of the project. Nevertheless, this build type "retroactively" affects on all properties described above.

Also, with multi-configuration build tools selected build type is appended to the location of output artifacts, like executables and libraries (see e.g. description of RUNTIME_OUTPUT_DIRECTORY target's property).

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Not sure I understand the last paragraph. Afaik, the value of `CMAKE_BUILD_TYPE` should have no effect for multi-configuration generators. This is also what [the documentation](https://cmake.org/cmake/help/v3.9/variable/CMAKE_BUILD_TYPE.html) states: *This variable is only meaningful to single-configuration generators*. – ComicSansMS Oct 19 '17 at 11:07
  • Can you expand a bit, perhaps with an example, on the different bullets? – einpoklum Oct 19 '17 at 11:51
  • I have edited description of the build type for multi-configuration tools. Hope things become clear now. – Tsyvarev Jul 23 '20 at 07:42