Build Types & Configurations
A build type (or configuration) is a named set of compiler flags. CMake ships four:
| Build type | Intent | GCC/Clang flags CMake adds |
|---|---|---|
Debug |
debugging | -g |
Release |
shipping, fastest code | -O3 -DNDEBUG |
RelWithDebInfo |
profiling, release with symbols | -O2 -g -DNDEBUG |
MinSizeRel |
smallest binary | -Os -DNDEBUG |
-DNDEBUG turns off assert(). With MSVC the flags differ (/Od /RTC1 plus debug information for Debug, /O2 /Ob2 /DNDEBUG for Release), which is one reason not to hard-code flags.
Seeing the flags
The flags live in cache variables named CMAKE_<LANG>_FLAGS_<CONFIG>. A quick way to see them, and the effect on a real compile:
mkdir -p bt && cd bt
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(BT LANGUAGES CXX)
foreach(cfg DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
message("CMAKE_CXX_FLAGS_${cfg} = '${CMAKE_CXX_FLAGS_${cfg}}'")
endforeach()
message("CMAKE_BUILD_TYPE = '${CMAKE_BUILD_TYPE}'")
add_executable(app main.cpp)
CM
cat > main.cpp <<'SRC'
#include <cassert>
#include <cstdio>
int main() {
#ifdef NDEBUG
std::puts("NDEBUG is set: asserts are off");
#else
std::puts("asserts are on");
#endif
assert(1 + 1 == 3);
std::puts("reached the end");
}
SRC
cmake -S . -B build-rel -G Ninja -DCMAKE_BUILD_TYPE=Release --log-level=NOTICE
cmake --build build-rel -v | grep -o '/usr/bin/c++ .* -c'
./build-rel/appCMAKE_CXX_FLAGS_DEBUG = '-g'
CMAKE_CXX_FLAGS_RELEASE = '-O3 -DNDEBUG'
CMAKE_CXX_FLAGS_RELWITHDEBINFO = '-O2 -g -DNDEBUG'
CMAKE_CXX_FLAGS_MINSIZEREL = '-Os -DNDEBUG'
CMAKE_BUILD_TYPE = 'Release'
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/bt/build-rel
/usr/bin/c++ -O3 -DNDEBUG -fno-diagnostics-color -MD -MT CMakeFiles/app.dir/main.cpp.o -MF CMakeFiles/app.dir/main.cpp.o.d -o CMakeFiles/app.dir/main.cpp.o -c
NDEBUG is set: asserts are off
reached the end
The assert that should have fired is gone in the Release build: that is -DNDEBUG at work.
The empty default
On single-config generators CMAKE_BUILD_TYPE is empty by default, and an empty build type adds no optimisation and no debug info. That surprises people: they benchmark a build with no -O at all.
mkdir -p bt && cd bt
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(BT LANGUAGES CXX)
add_executable(app main.cpp)
CM
cat > main.cpp <<'SRC'
#include <cassert>
int main() { assert(1 + 1 == 3); }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build -v | grep -o '/usr/bin/c++ .* -c'
./build/app 2>&1 | cat
echo "exit code: ${PIPESTATUS[0]}"/usr/bin/c++ -fno-diagnostics-color -MD -MT CMakeFiles/app.dir/main.cpp.o -MF CMakeFiles/app.dir/main.cpp.o.d -o CMakeFiles/app.dir/main.cpp.o -c
app: /home/learner/bt/main.cpp:2: int main(): Assertion `1 + 1 == 3' failed.
exit code: 134
134 is 128 + 6 (SIGABRT): assert aborted the program, because without a build type nothing defined NDEBUG.
You have three ways to deal with it:
- Always pass
-DCMAKE_BUILD_TYPE=…(or use a preset that sets it). Best for teams. - Set the
CMAKE_BUILD_TYPEenvironment variable (CMake 3.22+): it provides the default for new build directories. - Give your project a default, only when nothing was chosen and only for single-config generators:
get_property(is_multi GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT is_multi AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release RelWithDebInfo MinSizeRel)
endif()Never set(CMAKE_BUILD_TYPE Release) unconditionally: it overrides what the user asked for.
Per-configuration settings belong in generator expressions
When something must differ per configuration, write it once with a generator expression so it works with single- and multi-config generators:
target_compile_definitions(app PRIVATE lt;lt;CONFIG:Debug>:APP_TRACE_ALLOCATIONS>)
target_compile_options(app PRIVATE lt;lt;CONFIG:Release>:-fno-plt>)A model makes the selection logic concrete: given a configuration, which flags and definitions reach the compiler?
const builtin = {
Debug: ['-g'],
Release: ['-O3', '-DNDEBUG'],
RelWithDebInfo: ['-O2', '-g', '-DNDEBUG'],
MinSizeRel: ['-Os', '-DNDEBUG'],
'': [], // the empty default build type
};
// target_compile_definitions(app PRIVATE lt;lt;CONFIG:Debug>:APP_TRACE>)
const genexDefs = [{ when: (cfg) => cfg === 'Debug', value: '-DAPP_TRACE' }];
for (const cfg of ['Debug', 'Release', '']) {
const flags = [...builtin[cfg], ...genexDefs.filter((d) => d.when(cfg)).map((d) => d.value)];
console.log(`${JSON.stringify(cfg).padEnd(10)} c++ ${flags.join(' ')} -c main.cpp`.replace(/ +/g, ' '));
}"Debug" c++ -g -DAPP_TRACE -c main.cpp
"Release" c++ -O3 -DNDEBUG -c main.cpp
"" c++ -c main.cpp
Custom configurations
You can add your own configuration, for example Asan with AddressSanitizer, by defining CMAKE_<LANG>_FLAGS_ASAN and CMAKE_EXE_LINKER_FLAGS_ASAN, and (for multi-config generators) appending it to CMAKE_CONFIGURATION_TYPES. Many projects find it simpler to make sanitizers an option instead.
Test yourself
Which build type disables assert() and optimises with -O3 on GCC?
- Debug
- Release
- RelWithDebInfo
- The empty build type
You configure with Ninja and no CMAKE_BUILD_TYPE. What flags does CMake add?
- Debug flags
- Release flags
- No optimisation or debug flags at all
- It refuses to configure
What is the correct way to give a project a default build type?
set(CMAKE_BUILD_TYPE Release)at the top- Set it in the cache only if it is empty and the generator is single-config
- Edit CMakeCache.txt
- Pass
--config Releaseto cmake -S