Learning Hub Search Ctrl K

Modern C++ › C++20

Modules

import/export as a replacement for headers.

Since C++20 advanced #modules#import#export#header-units#build-system#bmi

Explanation

Modules replace the textual #include mechanism with compiled, importable units. A module is compiled once into a binary interface (BMI); importing it is far cheaper than re-parsing headers in every translation unit.

Differences from headers:

  • No macro leakage: macros defined in a module (or before an import) do not affect other code.
  • Explicit export: only declarations marked export are visible to importers; everything else is private, even without static/anonymous namespaces.
  • Order independence: import a; import b; behaves the same in either order.
  • No include guards and no ODR-via-header duplication problems.

Syntax:

export module geometry;           // module declaration (start of the interface unit)
export int area(int w, int h);    // export a declaration
export namespace shapes { ... }   // export a whole namespace
import <vector>;                  // import a header unit
import geometry;                  // in another file: use the module

A module can be split into an interface unit (export module m;), implementation units (module m;) and partitions (export module m:part;). The module name is not a namespace, so you still need namespaces for naming.

Example

Modules need build-system support, so this is shown as several files rather than one runnable program.

geometry.ixx (MSVC), geometry.cppm (Clang) or geometry.cc (GCC):

module;                                        // global module fragment: legacy includes go here
#include <cmath>
#include <string>

export module geometry;                        // the interface unit

export namespace geometry {
    struct Point { double x, y; };

    double distance(const Point& a, const Point& b) {
        return std::sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }

    std::string describe(const Point& p) {
        return "(" + std::to_string(p.x) + ", " + std::to_string(p.y) + ")";
    }
}

int hiddenHelper() { return 42; }              // not exported: invisible to importers

main.cpp:

#include <iostream>
import geometry;

int main() {
    geometry::Point a{0, 0}, b{3, 4};
    std::cout << geometry::distance(a, b) << " " << geometry::describe(b) << "\n";   // 5 (3.000000, 4.000000)
    // hiddenHelper();                         // error: not exported
}

Building:

Compiler Command (sketch)
MSVC cl /std:c++20 /EHsc /interface /TP geometry.ixx main.cpp (or add the files to a VS project; it orders builds automatically)
GCC 14+ g++ -std=c++20 -fmodules-ts -c geometry.cc main.cpp
Clang clang++ -std=c++20 --precompile geometry.cppm -o geometry.pcm, then compile main.cpp with -fmodule-file=geometry=geometry.pcm

CMake 3.28+ supports modules natively (target_sources(... FILE_SET CXX_MODULES FILES geometry.cppm)).

Common pitfalls

  • Tool support is the biggest hurdle: build systems must scan dependencies and compile modules before their importers; older Makefiles don’t. Mixed compiler versions can’t share BMIs.
  • A BMI is compiler- and flag-specific, not a distribution format.
  • Forgetting export on a declaration (or on a namespace member) makes it silently invisible: “identifier not found”.
  • Putting #include after the module declaration is an error; use the global module fragment (module;).
  • Macros you want to share (config flags) can’t be exported. Keep a small header for them.
  • Converting a large codebase is incremental: header units and import std; give most of the benefit first.

Legacy vs modern

// C++98..17: header + source, guards, macro pollution, repeated parsing
// point.h:  #ifndef POINT_H ... struct Point {...}; double distance(...); #endif
// point.cpp: #include "point.h" ...
// C++20: one module interface + optional implementation unit, no guards

The older workarounds were precompiled headers, unity builds and the Pimpl idiom. C++23 adds import std; (see std::print and import std).

Exercises

1. What does a module not replace?

Namespaces. A module name groups code for the build, but names inside still need a namespace to avoid collisions.