Search code examples
cmakerelease-buildsdebug-build

What does CMAKE_BUILD_TYPE affect, other than the compiler flag selection?


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?


Solution

  • 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).