Skip to content

Modules (C++20)

In a Nutshell

Replace header files with module interface files (.cppm) -- compile once and cache the result for dramatically faster recompilation, while isolating macro pollution and providing true symbol visibility control.

None (language feature, uses new file types and keywords)

Core API Quick Reference

SyntaxDescription
module;Global module fragment start (place #include and other preprocessor directives here)
export module mylib;Declares a module interface unit, exporting module name mylib
export int func();Export declaration, visible to module consumers
module mylib;Module implementation unit (not exported, implementation only)
import mylib;Import a module (replaces #include)
export import :sub;Re-export a sub-module
module :private;Private module fragment (C++20), implementation details not part of the module interface

Minimal Example

cpp
// Standard: C++20
// --- math.cppm (module interface) ---
export module math;

export int add(int a, int b) {
    return a + b;
}

// --- main.cpp (consumer) ---
import math;
#include <iostream>

int main() {
    std::cout << add(2, 3) << "\n"; // 5
}

Embedded Applicability: Medium

  • Build acceleration: module interfaces are compiled once and cached; large projects can see 30-70% recompilation time reduction
  • Macro isolation: #define directives outside the module boundary do not leak inside, improving build stability
  • Symbol visibility: export provides explicit API boundary control, replacing the "everything is public" model of headers
  • Build system support is still maturing: CMake's native module support is gradually improving in 3.28+
  • Cross-compiler compatibility issues exist (module BMI formats are not interchangeable), so cross-compiler builds require caution
  • Embedded toolchains (especially cross-compilation scenarios) lag in module support; not recommended for core embedded projects in the short term

Compiler Support

GCCClangMSVC
111619.28

See Also


Some content adapted from cppreference.com under CC-BY-SA 4.0 license

Built with VitePress