C++ · Module 17 · Lesson 61 min read

Modules (C++20) & Large Projects

Problems with #include

  • Headers are re-parsed in every .cpp file → slow builds
  • Macros leak from one header into everything after it
  • Include order can change meaning
  • Need include guards

A module

budget.cppm (module interface unit; extensions vary: .cppm, .ixx, .mpp)

arduino
module;                       // global module fragment: put #includes here
#include <string>
export module budget;         // the module's name

export double add_vat(double x) { return x * 1.13; }   // visible to importers
double helper();                                        // NOT exported: private to the module
export class Wallet { ... };

main.cpp

arduino
import budget;
int main() { add_vat(100); }
  • Compiled once into a binary module interface; importers load it quickly
  • Only exported names are visible
  • Macros don't leak out

Standard library modules (C++23)

import std; imports the entire standard library at once (support is arriving in compilers).

Building (compiler flags differ)

  • GCC: g++ -std=c++20 -fmodules-ts budget.cppm main.cpp
  • Clang: clang++ -std=c++20 --precompile budget.cppm -o budget.pcm, then use it
  • MSVC: .ixx files work in Visual Studio
  • CMake 3.28+ supports modules with FILE_SET CXX_MODULES Module support is still maturing — headers remain the most portable choice today.

Organising large projects (headers or modules)

scss
project/
├── CMakeLists.txt
├── include/budget/     (public headers)
├── src/                (implementation)
├── tests/
└── apps/main.cpp
  • One namespace per project (namespace budget { ... })
  • Small, focused files; one main class per file
  • Separate interface from implementation
  • Libraries for reusable parts, executables for apps

Note on this example

Built and run with GCC 14 (-fmodules-ts). The command differs for Clang and MSVC.

Example

C++
// ===== file: budget.cppm =====
module;
#include <string>
#include <vector>
export module budget;

double vat_rate() { return 0.13; }                 // not exported: private to the module

export double add_vat(double amount) { return amount * (1 + vat_rate()); }

export class Wallet {
public:
    explicit Wallet(std::string owner) : owner_(std::move(owner)) {}
    void add(double amount) { items_.push_back(amount); }
    double total() const {
        double t = 0;
        for (double x : items_) t += x;
        return t;
    }
    const std::string& owner() const { return owner_; }
private:
    std::string owner_;
    std::vector<double> items_;
};
// ===== file: main.cpp =====
#include <iostream>
import budget;

int main() {
    Wallet w("Asha");
    w.add(450);
    w.add(add_vat(1500));
    std::cout << w.owner() << "'s total: " << w.total() << '\n';
    std::cout << "add_vat(1000) = " << add_vat(1000) << '\n';
    // vat_rate();          // error: not exported from module 'budget'
    return 0;
}

/* Build (GCC 14):
g++ -std=c++20 -fmodules-ts -x c++ budget.cppm -x none main.cpp -o app
*/

/* Output:
Asha's total: 2145
add_vat(1000) = 1130
*/