telephone
telephone

Reputation: 1161

Using build type in CMake 3.x

In the 2.x version of CMake, the cmake option CMAKE_BUILD_TYPE controlled options to be passed to the compiler. For example, if CMAKE_BUILD_TYPE=RelWithDebInfo then the options passed to the compiler was CMAKE_CXX_FLAGS_RELWITHDEBINFO for C++ and CMAKE_C_FLAGS_RELWITHDEBINFO for C (http://binglongx.wordpress.com/tag/cmake_build_type/).

In the new 3.x version of CMake there are commands add_compile_options and target_compiler_options for adding options to the compiler (What is the modern method for setting general compile flags in CMake?).

Questions

How do I define which build type CMake should use? Is it still, for example, cmake -DCMAKE_BUILD_TYPE=Debug?

How do I make use of this build type in my CMakeLists.txt?

Has CMake defined all the build types, or can I define custom, additional, build types?

Has CMake set any default compiler options for each of the build types (for example -g for the Debug build type)?

Upvotes: 1

Views: 1329

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

Please try to stick to a single question per question in the future. You might risk getting closed as too broad otherwise. Also, you are more likely to get good answers for well-defined, precise questions.

How do I define which build type CMake should use? Is it still, for example, cmake -DCMAKE_BUILD_TYPE=Debug?

Yes, this did not change.

How do I make use of this build type in my CMakeLists.txt?

Use generator expressions. For example:

target_compile_options(my_program PUBLIC $<$<CONFIG:Debug>:-Werror>)
# Warnings are errors for debug build only

Has CMake defined all the build types, or can I define custom, additional, build types?

You can add your own type if the defaults don't cut it for you. Note though that this can be a bit fiddly, so I wouldn't do it unless you have good reasons. Take a look at CMAKE_CONFIGURATION_TYPES.

Has CMake set any default compiler options for each of the build types (for example -g for the Debug build type)?

CMake does a pretty good job at choosing the right defaults for the different configurations. In particular, Debug and RelWithDebInfo builds generate symbols correctly (-g on gcc) and Release builds are optimized quite well.

Upvotes: 3

Related Questions