
Cpp Modules
- 315 installs
- 155 repo stars
- Updated June 27, 2026
- mohitmishra786/low-level-dev-skills
Adopt C++20 modules to replace fragile header includes, speed incremental builds, and enforce clean component boundaries in large native codebases and toolchain-heavy projects.
About
Teaches C++20 modules for modular native builds: defining exportable interfaces, migrating from headers, configuring CMake or similar, and avoiding linkage surprises. Targets teams shipping CLI utilities and high-performance APIs.
- module interface versus partition files
- Header unit migration strategy
- Build system and compiler flags
- ODR and visibility across TU boundaries
- Incremental compile time gains
Cpp Modules by the numbers
- 315 all-time installs (skills.sh)
- +26 installs in the week ending Aug 4, 2026 (Skillselion tracking)
- Ranked #1,296 of 4,347 Backend & APIs skills by installs in the Skillselion catalog
- Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill cpp-modulesAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 315 |
|---|---|
| repo stars | ★ 155 |
| Last updated | June 27, 2026 |
| Repository | mohitmishra786/low-level-dev-skills ↗ |
What it does
Adopt C++20 modules to replace fragile header includes, speed incremental builds, and enforce clean component boundaries in large native codebases and toolchain-heavy projects.
Files
C++20 Modules
Purpose
Guide agents through authoring, building, and debugging C++20 modules: named modules vs header units, module partitions, CMake integration, compiler-specific flags, and interoperability with legacy headers.
Triggers
- "How do I write a C++20 module?"
- "How do I import a module in CMake?"
- "What's the difference between a named module and a header unit?"
- "My module gives 'cannot find module' errors"
- "How do I use C++20 modules with Clang?"
- "How do I migrate from headers to modules?"
Workflow
1. Module concepts overview
C++20 module kinds:
├── Named module interface unit (.cppm / .ixx) — exports declarations
├── Module implementation unit (.cpp) — defines module members
├── Module partition (.cppm) — internal module subdivision
└── Header unit (any header) — import a legacy header as moduleNamed modules are the primary target. Header units are a bridge for legacy code. Avoid Global Module Fragment unless required for macro access.
2. Named module — minimal example
// math.cppm — module interface unit
export module math; // declares the module name
export int add(int a, int b) { return a + b; }
export double pi = 3.14159;
// Non-exported (module-private)
int internal_helper() { return 42; }// main.cpp — consumer
import math; // import the module
#include <iostream> // legacy header (still works)
int main() {
std::cout << add(2, 3) << "\n"; // 5
std::cout << pi << "\n";
}3. Module partitions
// math-core.cppm — partition
export module math:core; // partition 'core' of module 'math'
export int add(int a, int b) { return a + b; }// math.cppm — primary module interface
export module math;
export import :core; // re-export the partition// math-impl.cpp — implementation unit (no export)
module math; // belongs to 'math' module, not a partition
// has access to all math declarations, but exports nothing4. Header units — bridging legacy headers
// Import a standard library header as a module unit
import <iostream>; // header unit (compiler generates BMI)
import <vector>;
// Or import a project header (must be compilable as header unit)
import "myheader.h";Header units do NOT provide macros to importers. For macro access, use the Global Module Fragment:
module; // Global Module Fragment starts here
#include <cassert> // macros like assert() are available
export module mymod;
// ... rest of module5. Building with Clang
# Compile module interface → produces .pcm (precompiled module)
clang++ -std=c++20 --precompile math.cppm -o math.pcm
# Compile implementation using the .pcm
clang++ -std=c++20 -fmodule-file=math=math.pcm -c math.cpp -o math.o
# Compile consumer
clang++ -std=c++20 -fmodule-file=math=math.pcm main.cpp math.o -o prog6. Building with GCC
# GCC ≥11 supports modules (experimental ≥11, better ≥14)
# Compile interface unit → produces .gcm in gcm.cache/
g++ -std=c++20 -fmodules-ts math.cppm -c -o math.o
# Compiler auto-discovers .gcm files in gcm.cache/
g++ -std=c++20 -fmodules-ts main.cpp math.o -o prog7. CMake integration (CMake ≥3.28)
cmake_minimum_required(VERSION 3.28)
project(myproject LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
add_library(math)
target_sources(math
PUBLIC
FILE_SET CXX_MODULES FILES # module interface units
src/math.cppm
src/math-core.cppm
PRIVATE
src/math-impl.cpp # implementation unit
)
add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE math)# Requires a generator that supports modules (Ninja ≥1.11 or MSBuild)
cmake -S . -B build -G Ninja
cmake --build buildFor CMake 3.25–3.27 (experimental):
cmake_minimum_required(VERSION 3.25)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "3c375311-a3c9-4396-a187-3227ef642046")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP ON)8. Common errors
| Error | Cause | Fix |
|---|---|---|
module 'math' not found | BMI not found in search path | Compile interface unit first; check -fmodule-file= flags |
cannot import header in module | #include inside module purview | Move #include to Global Module Fragment or use import <> |
redefinition of module 'math' | Two .cppm files declare same module | Only one primary interface per module |
macro not available after import | Macros don't cross module boundaries | Move macro-dependent code to GMF or use #include |
ODR violation | Same name in multiple partitions | Each name exported from exactly one partition |
| BMI cache stale | .pcm/.gcm not rebuilt after change | Clean build or ensure dependency tracking is working |
9. Interop with legacy headers
// Wrapping a C library for module use
export module cjson;
module; // Global Module Fragment
#include <cjson/cJSON.h> // C header with macros
export module cjson; // back to module purview
// Re-export key types (optional)
export using ::cJSON;
export using ::cJSON_Parse;For CMake module support details, see references/modules-cmake-support.md.
Related skills
- Use
skills/build-systems/build-accelerationfor PCH as a modules alternative - Use
skills/compilers/gccorskills/compilers/clangfor compiler-specific module flags - Use
skills/build-systems/cmakefor CMake project configuration
C++20 Modules CMake Support Reference
Source: https://cmake.org/cmake/help/latest/manual/cmake-cxxmodules.7.html
Table of Contents
1. Version Requirements 2. FILE_SET CXX_MODULES 3. Generator Support 4. Compiler Support Matrix
Version Requirements
| Feature | Minimum CMake |
|---|---|
Stable CXX_MODULES FILE_SET | 3.28 |
| Experimental modules (Ninja) | 3.25 |
import std; support | 3.30 (with MSVC STL) |
FILE_SET CXX_MODULES
add_library(mylib)
target_sources(mylib
PUBLIC
FILE_SET CXX_MODULES FILES
include/mylib.cppm # module interface
include/mylib-utils.cppm # partition
PRIVATE
src/mylib-impl.cpp # implementation unit (module; not export module)
)
# Install module interface units alongside the library
install(TARGETS mylib
ARCHIVE DESTINATION lib
FILE_SET CXX_MODULES DESTINATION include/mylib
)Generator Support
| Generator | Module Support | Notes |
|---|---|---|
| Ninja ≥1.11 | Full | Best support; use for development |
| Ninja Multi-Config | Full | CMake ≥3.28 |
| MSBuild (VS 2022) | Full | MSVC modules work well |
| Make | None | Does not support dynamic deps |
| Xcode | Partial | Experimental |
Always use Ninja for module builds during development:
cmake -S . -B build -G Ninja -DCMAKE_CXX_STANDARD=20Compiler Support Matrix
| Compiler | Status | Notes |
|---|---|---|
| MSVC 19.34+ (VS 2022 17.4+) | Best | import std; supported |
| Clang ≥16 | Good | Use --precompile pipeline |
| Clang ≥18 | Better | stdlib module support |
| GCC ≥14 | Improving | -fmodules-ts more stable |
| GCC 11–13 | Experimental | Many edge cases |
MSVC-specific
# Enable standard library modules with MSVC
target_compile_features(mylib PUBLIC cxx_std_23)
# In C++23: import std; // works with MSVC and CMake 3.30+Clang-specific flags
# Force Clang to use module compilation mode
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(mylib PRIVATE -fmodules)
endif()GCC-specific
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(mylib PRIVATE -fmodules-ts)
# GCC stores BMI in gcm.cache/ relative to build dir
endif()