Excellent question. Adding a custom configuration type is a common need for specialized builds, such as for testing, profiling, or sanitizers.
The process depends on a crucial distinction in CMake: whether you are using a multi-configuration generator (like Visual Studio, Xcode) or a single-configuration generator (like Makefiles, Ninja).
CMAKE_CONFIGURATION_TYPES is for multi-configuration generators.
CMAKE_BUILD_TYPE is for single-configuration generators.
I'll cover both scenarios, but let's start with the direct answer to your question.
1. The Direct Answer: Modifying CMAKE_CONFIGURATION_TYPES
This method is for multi-configuration generators (e.g., Visual Studio, Xcode, Ninja Multi-Config). These generators create project files that are aware of all build types simultaneously.
To add a custom configuration, you must set or modify the CMAKE_CONFIGURATION_TYPES variable before the first project() call in your top-level CMakeLists.txt.
Step 1: Add Your Custom Type to the List
In your CMakeLists.txt, add the following:
cmake
# Define the list of all configurations you want to support.
# This MUST be done BEFORE the project() command.
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;MinSizeRel;Sanitize"
CACHE STRING "Semicolon-separated list of supported configuration types." FORCE)
# Now, call project()
project(MyApp CXX)
Why CACHE STRING and FORCE?
CMake often pre-populates CMAKE_CONFIGURATION_TYPES with default values in a cache variable. Using set(... CACHE ... FORCE) ensures that your custom list overrides any default and is stored in the cache, making it persistent across re-runs of CMake.
Step 2: Define Flags for Your Custom Type
Now that CMake knows about your new configuration type (e.g., Sanitize), you need to tell it which compiler flags and definitions to use for that type.
CMake uses variables named CMAKE_<LANG>_FLAGS_<CONFIG> for this. The configuration name is uppercased.
cmake
# Flags for our custom "Sanitize" configuration
set(SANITIZE_FLAGS "-g -fsanitize=address,undefined -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_SANITIZE "${SANITIZE_FLAGS}"
CACHE STRING "Flags used by the C++ compiler for the Sanitize configuration.")
set(CMAKE_C_FLAGS_SANITIZE "${SANITIZE_FLAGS}"
CACHE STRING "Flags used by the C compiler for the Sanitize configuration.")
2. A Complete, Modern Example (Recommended Approach)
While modifying the global CMAKE_..._FLAGS_... variables works, the modern and more robust approach is to use generator expressions to set properties on a per-target basis. This is much cleaner and less prone to side effects.
Here is a complete CMakeLists.txt demonstrating the best practice.
CMakeLists.txt
cmake
cmake_minimum_required(VERSION 3.15)
# 1. Define the custom configuration list BEFORE project()
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Sanitize"
CACHE STRING "Semicolon-separated list of supported configuration types." FORCE)
project(MyAwesomeApp CXX)
# 2. Add your executable/library
add_executable(my_app main.cpp)
# 3. Use generator expressions to set properties per configuration
# This is the modern, preferred way.
# Set compile definitions for each configuration
target_compile_definitions(my_app PRIVATE
$<$<CONFIG:Debug>:DEBUG_MODE=1>
$<$<CONFIG:Release>:DEBUG_MODE=0>
$<$<CONFIG:Sanitize>:DEBUG_MODE=1;USING_ASAN=1>
)
# Set compile options (flags) for each configuration
target_compile_options(my_app PRIVATE
# No special flags for Debug/Release in this example (CMake adds defaults)
$<$<CONFIG:Sanitize>:-g;-fsanitize=address,undefined;-fno-omit-frame-pointer>
)
Configure CMake (e.g., for Visual Studio or Xcode):
cmake -G "Visual Studio 17 2022" ..
cmake -G "Xcode" ..
Open the generated solution/project. You will see "Sanitize" in the configuration dropdown menu.
Alternatively, build from the command line:
bash
# Build the Sanitize configuration
cmake --build . --config Sanitize
# Build the Debug configuration
cmake --build . --config Debug
3. Important: The Single-Configuration Generator Case
What if you are using Makefiles or Ninja (the default on Linux)? These are single-configuration generators. CMAKE_CONFIGURATION_TYPES has no effect for them.
Instead, you specify the build type at configure time using CMAKE_BUILD_TYPE.
To support a custom configuration here, you don't need to modify CMAKE_CONFIGURATION_TYPES. You simply need to:
Define the flags for your custom type.
Pass your custom type name via -D CMAKE_BUILD_TYPE=...
CMakeLists.txt for Single-Config Generators
You can create a CMakeLists.txt that works for both generator types.
cmake
cmake_minimum_required(VERSION 3.15)
project(MyAwesomeApp CXX)
# This is good practice for single-config generators.
# It validates CMAKE_BUILD_TYPE and sets a default if none is provided.
set(VALID_CONFIGS Debug Release RelWithDebInfo MinSizeRel Sanitize)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${VALID_CONFIGS})
endif()
# Define the flags for the custom type (must be uppercase)
set(CMAKE_CXX_FLAGS_SANITIZE "-g -fsanitize=address,undefined -fno-omit-frame-pointer"
CACHE STRING "Flags for Sanitize build type")
# The rest is the same! Generator expressions work perfectly for both.
add_executable(my_app main.cpp)
target_compile_definitions(my_app PRIVATE
$<$<CONFIG:Debug>:DEBUG_MODE=1>
$<$<CONFIG:Release>:DEBUG_MODE=0>
$<$<CONFIG:Sanitize>:DEBUG_MODE=1;USING_ASAN=1>
)
target_compile_options(my_app PRIVATE
$<$<CONFIG:Sanitize>:-g;-fsanitize=address,undefined;-fno-omit-frame-pointer>
)
How to Build (with a Single-Config Generator)
mkdir build && cd build
Configure CMake, passing the custom build type:
bash
# Configure for a Sanitize build
cmake -G "Ninja" -D CMAKE_BUILD_TYPE=Sanitize ..
# Or for a Debug build
# cmake -G "Ninja" -D CMAKE_BUILD_TYPE=Debug ..
Build the project. The configuration is already "baked in".
bash
cmake --build .
# or just `ninja`
Summary
Feature
Multi-Config (Visual Studio, Xcode)
Single-Config (Makefiles, Ninja)
Control Variable
CMAKE_CONFIGURATION_TYPES
CMAKE_BUILD_TYPE
When to Set
Before project() call (in CMakeLists.txt)
At configure time (with -D CMAKE_BUILD_TYPE=...)
How to Change Build
cmake --build . --config <Type>
Re-run cmake -D CMAKE_BUILD_TYPE=...
Recommended Practice
Define CMAKE_CONFIGURATION_TYPES and use generator expressions for target properties.
Check CMAKE_BUILD_TYPE, define CMAKE_<LANG>_FLAGS_<CONFIG> variables, and use generator expressions.
User
how to inherit custom configuration from Debug configuration?
Assistant
Of course! This is a very common and useful pattern. You often want a configuration that is "like Debug, but with one extra thing" (e.g., profiling, a specific sanitizer, more verbose logging).
The concept of "inheritance" in this context means copying the settings from the Debug configuration and then adding or modifying them.
The key trick is to do this after the project() command, because project() is what causes CMake to populate the default Debug flag variables (like CMAKE_CXX_FLAGS_DEBUG).
Let's create a custom configuration called DebugWithProfile that inherits from Debug and adds profiling flags (-pg).
Step-by-Step Guide
Here is the robust, recommended way to do it. This method works for both multi-configuration and single-configuration generators.
Step 1: Add the Custom Configuration to the List (Before project())
As before, if you are using a multi-configuration generator (Visual Studio, Xcode), you must declare your new configuration type in CMAKE_CONFIGURATION_TYPESbefore the project() call.
cmake
# CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
# 1. Define all configurations, including your new custom one.
# This MUST be done BEFORE the project() command.
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;DebugWithProfile"
CACHE STRING "Semicolon-separated list of supported configuration types." FORCE)
Step 2: Call project()
This is the critical step that initializes CMake's default toolchain variables, including CMAKE_CXX_FLAGS_DEBUG.
cmake
# 2. Call project(). This populates CMAKE_<LANG>_FLAGS_DEBUG.
project(MyProfilerApp CXX C)
Step 3: Inherit and Extend the Flags (After project())
Now that CMAKE_CXX_FLAGS_DEBUG and CMAKE_C_FLAGS_DEBUG have been set by CMake, you can read them, append your own flags, and set the variables for your new DebugWithProfile configuration.
The configuration name in the variable must be . ( becomes ).
UPPERCASED
DebugWithProfile
DEBUGWITHPROFILE
cmake
# 3. Define the flags for the new configuration by inheriting from Debug.
message(STATUS "Initial CXX Debug flags: ${CMAKE_CXX_FLAGS_DEBUG}")
# Define the extra flags you want to add
set(PROFILING_FLAGS "-pg") # For gprof
# Inherit and append for C++
set(CMAKE_CXX_FLAGS_DEBUGWITHPROFILE "${CMAKE_CXX_FLAGS_DEBUG} ${PROFILING_FLAGS}"
CACHE STRING "Flags for C++ DebugWithProfile build." FORCE)
# Inherit and append for C
set(CMAKE_C_FLAGS_DEBUGWITHPROFILE "${CMAKE_C_FLAGS_DEBUG} ${PROFILING_FLAGS}"
CACHE STRING "Flags for C DebugWithProfile build." FORCE)
A Complete, Robust CMakeLists.txt Example
This example puts everything together and uses a loop to handle all languages enabled in your project automatically. This is the best practice.
cmake
cmake_minimum_required(VERSION 3.15)
# --- Step 1: Define Configurations (Before project) ---
# For multi-config generators like Visual Studio, Xcode, Ninja Multi-Config
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;MinSizeRel;DebugWithProfile"
CACHE STRING "Semicolon-separated list of supported configuration types." FORCE)
# --- Step 2: Call project() ---
# This discovers the compiler and sets default flags for Debug, Release, etc.
project(MyProfilerApp CXX C)
# --- Step 3: Inherit and Extend Flags (After project) ---
# This is the core logic for "inheritance".
# We do this for every language enabled in the project (e.g., C, CXX).
message(STATUS "Setting up custom DebugWithProfile configuration...")
foreach(LANG ${CMAKE_PROJECT_LANGUAGES})
# Construct the variable names for the source (Debug) and destination (DebugWithProfile)
set(DEBUG_FLAGS_VAR "CMAKE_${LANG}_FLAGS_DEBUG")
set(CUSTOM_FLAGS_VAR "CMAKE_${LANG}_FLAGS_DEBUGWITHPROFILE")
# Define the extra flags we want to add
set(PROFILING_FLAGS "-pg") # Example for gprof profiling
# Set the custom flags by combining the debug flags and our new flags
set(${CUSTOM_FLAGS_VAR} "${${DEBUG_FLAGS_VAR}} ${PROFILING_FLAGS}"
CACHE STRING "Flags for ${LANG} in DebugWithProfile configuration." FORCE)
message(STATUS " - ${CUSTOM_FLAGS_VAR}: ${${CUSTOM_FLAGS_VAR}}")
endforeach()
# --- Your Project Targets ---
add_executable(my_app main.c)
# You can still use generator expressions for other things!
target_compile_definitions(my_app PRIVATE
$<$<CONFIG:Debug,DebugWithProfile>:MYAPP_DEBUG=1>
$<$<CONFIG:Release,RelWithDebInfo>:MYAPP_DEBUG=0>
$<$<CONFIG:DebugWithProfile>:MYAPP_PROFILE=1>
)
How to Build
The process is the same as before, you just use your new configuration name.
For Multi-Configuration Generators (Visual Studio, Xcode)
mkdir build && cd build
cmake -G "Ninja Multi-Config" .. (or your preferred generator)
You will see a message like: Setting up custom DebugWithProfile configuration...
Build the new configuration:
bash
cmake --build . --config DebugWithProfile
After running, you will have an executable compiled with both debug symbols (-g) and profiling information (-pg).
For Single-Configuration Generators (Makefiles, Ninja)
mkdir build && cd build
Configure CMake, specifying your custom build type:
bash
cmake -D CMAKE_BUILD_TYPE=DebugWithProfile ..
You will see the same status messages as CMake sets up the flags.
Build:
bash
cmake --build .
This will produce a single executable compiled with the combined Debug and Profiling flags.
Summary of the "Inheritance" Logic
Declare: Add the new configuration name to CMAKE_CONFIGURATION_TYPES (before project()).
Initialize: Call project() to let CMake set up default flags (e.g., -g for CMAKE_CXX_FLAGS_DEBUG).
Inherit: After project(), read the value from the base configuration's flag variable (e.g., CMAKE_CXX_FLAGS_DEBUG).
Extend: Append your custom flags and set the variable for your new configuration (e.g., CMAKE_CXX_FLAGS_DEBUGWITHPROFILE).