Header Cost & Build Analyzers
A header's cost to the build is roughly:
cost(header) ≈ (lines it expands to) × (number of TUs that include it)A 30 000-line header included by two files is cheap. A 3 000-line project header included by 500 files is expensive, and every edit to it triggers 500 recompiles. Build analyzers such as ClangBuildAnalyzer rank headers by measured time summed over all TUs. This lesson builds the same ranking from two deterministic measures that every compiler offers: preprocessed line counts (-E | wc -l) and the include lists recorded by Ninja (ninja -t deps).
Seeing the include tree: -H
-H makes GCC and Clang print every header they open, indented by depth. Counting the lines gives the number of headers a TU reads:
mkdir -p h && cd h
printf '#include <string>\nint f() { return std::string("x").size(); }\n' > uses_string.cpp
printf 'int g() { return 1; }\n' > plain.cpp
for f in plain uses_string; do
n=$(g++ -std=c++20 -fsyntax-only -H $f.cpp 2>&1 | grep -c '^\.')
lines=$(g++ -std=c++20 -E $f.cpp | wc -l)
echo "$f.cpp: $n headers, $lines preprocessed lines"
done
echo "--- the first levels of the tree for uses_string.cpp:"
g++ -std=c++20 -fsyntax-only -H uses_string.cpp 2>&1 | grep -E '^\.{1,2} ' | head -6 | sed 's#/usr/include/c++/13/##; s#/usr/include/x86_64-linux-gnu/c++/13/##'plain.cpp: 0 headers, 7 preprocessed lines
uses_string.cpp: 163 headers, 31828 preprocessed lines
--- the first levels of the tree for uses_string.cpp:
. string
.. bits/requires_hosted.h
.. bits/stringfwd.h
.. bits/char_traits.h
.. bits/allocator.h
.. bits/cpp_type_traits.h
One #include <string> costs 163 header files and about 32 000 lines, every time.
A deterministic build analyzer
The project below has six source files and four project headers. One of them, common.hpp, is the classic "everything header" that drags <map>, <string>, <vector> and <algorithm> into every file. The script builds the project, reads which project headers each object depends on from ninja -t deps, measures each header's preprocessed size, and ranks them:
mkdir -p an/src && cd an
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Analyze CXX)
file(GLOB SRCS CONFIGURE_DEPENDS src/*.cpp)
add_executable(app ${SRCS})
CM
cat > src/common.hpp <<'H'
#pragma once
#include <algorithm>
#include <map>
#include <string>
#include <vector>
H
printf '#pragma once\nstruct Point { int x, y; };\n' > src/point.hpp
printf '#pragma once\n#include <regex>\nbool valid_name(const char*);\n' > src/names.hpp
printf '#pragma once\nint render(int);\n' > src/render.hpp
printf '#include "common.hpp"\n#include "render.hpp"\nint main() { return render(0); }\n' > src/main.cpp
printf '#include "common.hpp"\n#include "point.hpp"\n#include "render.hpp"\nint render(int v) { return v; }\n' > src/render.cpp
printf '#include "common.hpp"\n#include "point.hpp"\nint physics() { return Point{1, 2}.x; }\n' > src/physics.cpp
printf '#include "common.hpp"\nint audio() { return 0; }\n' > src/audio.cpp
printf '#include "common.hpp"\n#include "names.hpp"\nbool valid_name(const char* s) { return std::regex_match(s, std::regex("[a-z]+")); }\n' > src/names.cpp
printf '#include "common.hpp"\nint save() { return 0; }\n' > src/save.cpp
cmake -S . -B build -G Ninja > /dev/null && cmake --build build > /dev/null
cd build
echo "TUs: $(ninja -t deps | grep -c '\.o: #deps')"
ninja -t deps | awk '/^ .*\/src\/.*\.hpp$/ { sub(".*/src/", ""); n[$0]++ } END { for (h in n) print h, n[h] }' | sort > includers.txt
while read -r header tus; do
lines=$(echo "#include \"$header\"" | g++ -std=c++20 -x c++ -I ../src -E - | wc -l)
echo "$header $tus $lines $((tus * lines))"
done < includers.txt | sort -k4 -nr | awk 'BEGIN { printf "%-11s %4s %8s %10s\n", "header", "TUs", "lines", "TUs*lines" }
{ printf "%-11s %4d %8d %10d\n", $1, $2, $3, $4 }'TUs: 6
header TUs lines TUs*lines
common.hpp 6 51822 310932
names.hpp 1 78117 78117
render.hpp 2 10 20
point.hpp 2 10 20
common.hpp dominates: it's not the biggest header, but it's included everywhere. names.hpp is the biggest per include (it pulls <regex>), yet it costs less in total because only one file uses it. point.hpp and render.hpp are noise.
That ranking tells you what to do:
| Finding | Fix |
|---|---|
| A big header included by most TUs | split it, or move it into a precompiled header |
| A big header included by a few TUs | leave it, or hide it behind a function (names.hpp doesn't need <regex> in its interface) |
| A small header included everywhere and edited often | keep it tiny; it decides your incremental build cost |
The second fix is worth a look: names.hpp declares bool valid_name(const char*), which doesn't need <regex> at all. Moving the include into names.cpp costs nothing and removes the header weight from every future includer. Forward declarations, Pimpl & IWYU is about doing that systematically.
Line counts vs real time
Preprocessed lines are a proxy. Template-heavy code (<regex>, Boost, <ranges>, fmt) costs more per line than plain declarations, and some headers are mostly macros. Use line counts and include counts to find candidates deterministically and to prove a change helped; use -ftime-trace and ClangBuildAnalyzer to confirm where the seconds go.
Test yourself
Header A expands to 40 000 lines and is included by 2 TUs. Header B expands to 5 000 lines and is included by 60 TUs. Which costs more over a clean build?
- A (80 000 lines)
- B (300 000 lines)
- They are equal
- Neither, headers are free
What does g++ -H print?
- Help text
- Every header opened, indented by include depth
- Hot functions
- The hash of the TU
names.hpp includes <regex> but only declares bool valid_name(const char*). What's the cheap fix?
- Put names.hpp in every TU
- Move
#include <regex>into names.cpp - Use a unity build
- Disable optimisation