configure_file & Generated Headers
Programs often need build-time facts in their source code: the version number, the git commit, whether an optional feature was enabled. configure_file() copies a template file into the build directory and replaces placeholders with CMake variable values, at configure time.
configure_file(<input> <output> [@ONLY] [COPYONLY] [NEWLINE_STYLE UNIX])| In the template | Becomes |
|---|---|
@VAR@ |
the value of VAR |
${VAR} |
the value of VAR (unless @ONLY is given, which is safer for files containing ${…} of their own, like shell scripts) |
#cmakedefine VAR |
#define VAR if VAR is true, else /* #undef VAR */ |
#cmakedefine VAR @VAR@ |
#define VAR <value> if true, else /* #undef VAR */ |
#cmakedefine01 VAR |
#define VAR 1 or #define VAR 0 |
A version and feature header
mkdir -p app/src && cd app
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(App VERSION 3.1.4 DESCRIPTION "Demo app" LANGUAGES CXX)
option(APP_WITH_COLOR "Colored output" ON)
option(APP_WITH_NETWORK "Network support" OFF)
option(APP_WITH_SOUND "Sound support" OFF)
set(APP_MAX_USERS 64)
configure_file(src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/generated/app/config.h @ONLY)
add_executable(app src/main.cpp)
target_include_directories(app PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/generated)
CM
cat > src/config.h.in <<'SRC'
#pragma once
#define APP_NAME "@PROJECT_NAME@"
#define APP_VERSION "@PROJECT_VERSION@"
#define APP_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#cmakedefine APP_WITH_COLOR
#cmakedefine APP_WITH_SOUND
#cmakedefine APP_MAX_USERS @APP_MAX_USERS@
#cmakedefine01 APP_WITH_NETWORK
SRC
cat > src/main.cpp <<'SRC'
#include <cstdio>
#include <app/config.h>
int main() {
std::printf("%s %s (major %d), max users %d\n", APP_NAME, APP_VERSION, APP_VERSION_MAJOR, APP_MAX_USERS);
#ifdef APP_WITH_COLOR
std::puts("color: on");
#endif
#if !APP_WITH_NETWORK
std::puts("network: compiled out");
#endif
}
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cat build/generated/app/config.h
cmake --build build > /dev/null && ./build/app#pragma once
#define APP_NAME "App"
#define APP_VERSION "3.1.4"
#define APP_VERSION_MAJOR 3
#define APP_WITH_COLOR
/* #undef APP_WITH_SOUND */
#define APP_MAX_USERS 64
#define APP_WITH_NETWORK 0
App 3.1.4 (major 3), max users 64
color: on
network: compiled out
Put generated headers in the build directory, never back into the source tree: several build directories (Debug, Release, another compiler) can then coexist, each with its own config.h. Including it as <app/config.h> from a generated/ include directory keeps a project-specific prefix, so it can't clash with another library's config.h.
Only rewritten when the content changes
configure_file rewrites the output only if the result differs from the existing file. That matters: the header's timestamp drives rebuilds. Re-configuring without changes rebuilds nothing; changing an option rebuilds exactly the files that include the header:
mkdir -p app && cd app
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(App LANGUAGES CXX)
option(APP_FAST "Fast mode" OFF)
configure_file(config.h.in generated/config.h @ONLY)
add_executable(app main.cpp other.cpp)
target_include_directories(app PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/generated)
CM
echo '#cmakedefine01 APP_FAST' > config.h.in
cat > main.cpp <<'SRC'
#include <cstdio>
#include "config.h"
int main() { std::printf("fast mode: %d\n", APP_FAST); }
SRC
echo 'int other() { return 0; }' > other.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build > /dev/null && ./build/app
echo "--- re-configure, nothing changed"
cmake -S . -B build --log-level=NOTICE > /dev/null && cmake --build build
echo "--- turn the option on"
cmake -S . -B build --log-level=NOTICE -DAPP_FAST=ON > /dev/null && cmake --build build && ./build/appfast mode: 0
--- re-configure, nothing changed
ninja: no work to do.
--- turn the option on
[1/2] Building CXX object CMakeFiles/app.dir/main.cpp.o
[2/2] Linking CXX executable app
fast mode: 1
Only main.cpp, which includes the header, was recompiled.
Other uses
- Non-C files: scripts,
.pcfiles for pkg-config, Doxygen configuration,Info.pliston macOS,.rcversion resources on Windows. Use@ONLYfor anything that has its own${…}syntax. - COPYONLY copies a file into the build tree without substitutions, and re-copies it when it changes (CMake also re-runs the configure when the input changes).
- Generate-time values:
configure_filecan't evaluate generator expressions. For contents that depend on the configuration or target paths, usefile(GENERATE OUTPUT … CONTENT …)(see Generator Expressions), or both:configure_filefirst, thenfile(GENERATE INPUT …).
The git commit: a build-time job
A version from project() changes only when you edit CMakeLists.txt. The git commit changes all the time, and configure_file only runs when CMake re-configures, so a header made with it would be stale after the next commit. That needs a step that runs on every build, which is an add_custom_command/add_custom_target job: see add_custom_target & Host Tools.
Test yourself
What does #cmakedefine APP_WITH_NETWORK become when the option is OFF?
#define APP_WITH_NETWORK 0/* #undef APP_WITH_NETWORK */- Nothing at all
#define APP_WITH_NETWORK OFF
Why should generated headers go into the build directory?
- The source directory is read-only
- Several build directories can each have their own version, and the source tree stays clean
- configure_file can't write elsewhere
- IDEs require it
You re-run cmake without changing anything. Does the configured header get a new timestamp?
- Yes, always
- No: configure_file rewrites the output only when the content changes, so nothing recompiles
- Only with @ONLY
- Only in Release