Cpp Modules

mohitmishra786/low-level-dev-skills/skills/compilers/cpp-modules

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

C++20 modules skill for modern C++ projects. Use when working with named modules, module partitions, header units, CMake MODULE_SOURCES, Clang -fmodules-ts, BMI caching issues, or migrating from headers to modules. Activates on queries about C++20 modules, import statements, module interface units, header units, or BMI files.

AI 產生的概覽

引導代理撰寫、建置與偵錯 C++20 模組,涵蓋 CMake 與編譯器整合。

功能
此技能提供處理 C++20 模組的指示:具名模組介面單元、模組實作單元、分割區與標頭單元。內容包含最簡程式碼範例、Clang 與 GCC 的編譯器專用建置指令、CMake 3.25 至 3.28 及更新版本的整合方式,以及常見模組錯誤與修正對照表。它也說明與舊式標頭檔的互通性以及全域模組片段。
適用情境
適用於撰寫或匯入 C++20 模組、排查缺少 BMI 或重複定義等模組建置錯誤、為模組原始碼設定 CMake,或將程式碼基底從標頭檔遷移至模組的情況。
執行需求
需要支援 C++20 的編譯器,例如 Clang 或 GCC 11 以上版本;可選用 CMake 3.25 以上版本,並搭配支援模組的產生器(如 Ninja 1.11 或 MSBuild)。此技能不含指令碼,僅為指示文件,並附有一份關於 CMake 模組支援的參考文件。

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 module

Named 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

cpp
// math.cppm — module interface unitexport 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; }
cpp
// main.cpp — consumerimport 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

cpp
// math-core.cppm — partitionexport module math:core;     // partition 'core' of module 'math'
export int add(int a, int b) { return a + b; }
cpp
// math.cppm — primary module interfaceexport module math;export import :core;         // re-export the partition
cpp
// math-impl.cpp — implementation unit (no export)module math;                 // belongs to 'math' module, not a partition// has access to all math declarations, but exports nothing

4. Header units — bridging legacy headers

cpp
// Import a standard library header as a module unitimport <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:

cpp
module;                      // Global Module Fragment starts here#include <cassert>           // macros like assert() are availableexport module mymod;// ... rest of module

5. Building with Clang

bash
# Compile module interface → produces .pcm (precompiled module)clang++ -std=c++20 --precompile math.cppm -o math.pcm
# Compile implementation using the .pcmclang++ -std=c++20 -fmodule-file=math=math.pcm -c math.cpp -o math.o
# Compile consumerclang++ -std=c++20 -fmodule-file=math=math.pcm main.cpp math.o -o prog

6. Building with GCC

bash
# 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 prog

7. CMake integration (CMake ≥3.28)

cmake
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)
bash
# Requires a generator that supports modules (Ninja ≥1.11 or MSBuild)cmake -S . -B build -G Ninjacmake --build build

For CMake 3.25–3.27 (experimental):

cmake
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

ErrorCauseFix
module 'math' not foundBMI not found in search pathCompile interface unit first; check -fmodule-file= flags
cannot import header in module#include inside module purviewMove #include to Global Module Fragment or use import <>
redefinition of module 'math'Two .cppm files declare same moduleOnly one primary interface per module
macro not available after importMacros don't cross module boundariesMove macro-dependent code to GMF or use #include
ODR violationSame name in multiple partitionsEach name exported from exactly one partition
BMI cache stale.pcm/.gcm not rebuilt after changeClean build or ensure dependency tracking is working

9. Interop with legacy headers

cpp
// Wrapping a C library for module useexport 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 [blocked].

Related skills

  • Use skills/build-systems/build-acceleration for PCH as a modules alternative
  • Use skills/compilers/gcc or skills/compilers/clang for compiler-specific module flags
  • Use skills/build-systems/cmake for CMake project configuration

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/compilers/cpp-modules提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架