TExcelTAD

C++20 Modules & the Build Graph

C++20 named modules replace textual inclusion for code that adopts them. A module interface is compiled once into a binary module interface (BMI: .pcm for Clang, .gcm for GCC, .ifc for MSVC), and every importer loads that BMI instead of re-parsing source text. In principle that's a precompiled header for every library, without macro leakage or include-order problems.

The catch is the build system. With headers, every TU can compile in any order: #include is resolved by the compiler as it goes. With modules, a TU that says import math; can't be compiled until math's BMI exists. The build graph gets new edges, and they are written inside the source files, so the build system has to discover them before it can schedule anything.

Modules by hand with Clang 18

bash · try it yourself
mkdir -p mods && cd mods
cat > math.cppm <<'SRC'
export module math;
export int square(int x) { return x * x; }
SRC
cat > main.cpp <<'SRC'
import math;
#include <cstdio>
int main() { std::printf("square(7) = %d\n", square(7)); }
SRC
echo "--- compile the importer first:"
clang++ -std=c++20 -c main.cpp -o main.o 2>&1 | grep error
echo "--- correct order: interface -> BMI, then importers, then objects, then link"
clang++ -std=c++20 --precompile math.cppm -o math.pcm
clang++ -std=c++20 -fmodule-file=math=math.pcm -c main.cpp -o main.o
clang++ -std=c++20 -c math.pcm -o math.o
clang++ main.o math.o -o app && ./app
--- compile the importer first:
main.cpp:1:8: fatal error: module 'math' not found
1 error generated.
--- correct order: interface -> BMI, then importers, then objects, then link
square(7) = 49

Four steps instead of two, with a strict order: math.pcm must exist before main.cpp compiles. Every compile of an importer also has to be told where each BMI lives (-fmodule-file=name=path). Multiply that by hundreds of modules and you need the build system to work it out.

GCC 13 uses a different scheme: -fmodules-ts writes BMIs into a gcm.cache/ directory, and importers look there:

bash · try it yourself
mkdir -p gmods && cd gmods
printf 'export module math;\nexport int square(int x) { return x * x; }\n' > math.cc
printf 'import math;\nint main() { return square(3) - 9; }\n' > main.cc
g++ -std=c++20 -fmodules-ts -c main.cc -o main.o 2>&1 | grep -o 'error: .*' | head -1
g++ -std=c++20 -fmodules-ts -c math.cc -o math.o
g++ -std=c++20 -fmodules-ts -c main.cc -o main.o
g++ main.o math.o -o app && ./app && echo "exit 0"
ls gcm.cache
error: failed to read compiled module: No such file or directory
exit 0
math.gcm

How CMake builds modules: dependency scanning

CMake (3.28+) supports named modules with the Ninja generators and Visual Studio. You list interface files in a CXX_MODULES file set:

cmake
cmake_minimum_required(VERSION 3.28)
project(Mods CXX)
set(CMAKE_CXX_STANDARD 20)
add_executable(app main.cpp)
target_sources(app PRIVATE FILE_SET CXX_MODULES FILES math.cppm)

At build time, CMake first runs a scanner over every source to produce a P1689 JSON file ("this file provides math and imports nothing", "this file imports math"). A collate step turns those into Ninja dyndep files, and only then do the real compiles run in the right order. Scanning needs compiler support:

Compiler Scanner CMake support
Clang ≥ 16 clang-scan-deps (a separate tool) yes
GCC ≥ 14 built-in (-fdeps-format=p1689r5) yes
GCC 13 none no
MSVC ≥ 19.34 (VS 17.4) built-in (/scanDependencies) yes

The image used for this course has Clang 18 without the separate clang-scan-deps package, and GCC 13. So it shows exactly what happens when scanning isn't available, which is a common first experience with modules:

bash · try it yourself
mkdir -p cm && cd cm
printf 'export module math;\nexport int square(int x) { return x * x; }\n' > math.cppm
printf 'import math;\nint main() { return square(3) - 9; }\n' > main.cpp
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.28)
project(Mods CXX)
set(CMAKE_CXX_STANDARD 20)
add_executable(app main.cpp)
target_sources(app PRIVATE FILE_SET CXX_MODULES FILES math.cppm)
CM
echo "=== GCC 13"
cmake -S . -B gcc -G Ninja 2>&1 | sed -n '/CMake Error/,/SCAN_FOR_MODULES/p'
echo "=== Clang 18 without clang-scan-deps"
CXX=clang++ cmake -S . -B clang -G Ninja > /dev/null
cmake --build clang 2>&1 | grep -E '^\[|not found'
=== GCC 13
CMake Error in CMakeLists.txt:
  The target named "app" has C++ sources that may use modules, but the
  compiler does not provide a way to discover the import graph dependencies.
  See the cmake-cxxmodules(7) manual for details.  Use the
  CMAKE_CXX_SCAN_FOR_MODULES variable to enable or disable scanning.
=== Clang 18 without clang-scan-deps
[1/6] Scanning /home/learner/cm/main.cpp for CXX dependencies
/bin/sh: 1: CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS-NOTFOUND: not found

With clang-tools-18 installed (which provides clang-scan-deps-18), or with GCC 14+, the same CMakeLists.txt builds: the [1/6] Scanning… step succeeds and the build graph grows a scan and a collate step per target. That extra scanning is part of the price of modules.

import std;

C++23's import std; imports the whole standard library as one module. It needs a standard library that ships module sources: libc++ 17+ (Clang) or MSVC's STL, and GCC 15+ with libstdc++. CMake (3.30 and still in 4.4) supports it only behind the experimental CMAKE_EXPERIMENTAL_CXX_IMPORT_STD gate, after which CMAKE_CXX_MODULE_STD/CXX_MODULE_STD request it for cxx_std_23 targets; this image has libstdc++ 13 and no libc++, so import std can't be shown running here.

What modules mean for build performance

  • Clean builds: big potential savings, the same mechanism as PCH (parse once, load many times), but per library and without force-including everything everywhere. Reported gains vary widely with how much of the code base and its dependencies are modularised.
  • Critical path: new serial edges. A module imported by everything must compile first, and importers wait for it. Deep import chains lengthen the critical path.
  • Incremental builds: changing a module interface rebuilds its importers (like a header). Changing a module implementation unit doesn't.
  • Caching and distribution: BMIs are compiler- and flag-specific, and every compile needs the BMIs it imports. ccache counts module compiles as could_not_use_modules and doesn't cache them; distributed builds must ship BMIs to workers.
  • Tooling: scanning tools, IDE support and package managers are still catching up; mixing headers and modules (import <vector>; header units) is even less portable.

A pragmatic path today: keep headers + PCH for the bulk of the code, try modules on a new, self-contained library with a compiler that supports scanning, and measure.

Test yourself

Why can't a build system compile module importers in any order, like it does with #include?

  1. Modules are slower to parse
  2. An importer needs the imported module's BMI, which must be built first
  3. Modules can't be compiled in parallel at all
  4. Ninja doesn't support C++20

What does CMake run before compiling sources in a target with CXX_MODULES?

  1. The linker
  2. A dependency scanner that writes P1689 files, then a collate step
  3. clang-format
  4. ccache

With GCC 13, what happens when you configure a CMake target with a CXX_MODULES file set?

  1. It builds normally
  2. CMake errors: the compiler can't discover the import graph
  3. GCC falls back to headers
  4. Ninja ignores the modules