On this page
  1. Compilation, Headers, Modules & ABI
  2. Overview
  3. Headers, Include Guards & ODR
  4. C++20 Modules
  5. Name Mangling & extern "C" ABI Boundaries
  6. Recommended Baseline Compiler Flags

Compilation, Headers, Modules & ABI

Prev: Templates, Concepts & Polymorphism | Up: Language & RAII | Next: tutorials
Rel: C Compilation & Internals, Namespaces, Compilation, Interpretation & JIT
Ref: cppreference — Modules (C++20) | cppreference — Language Linkage
Tags: #public #prog #cpp #compilation #abi #modules #tier/standard


Overview

C++ builds on C's separate compilation model (preprocessor → compiler → assembler → linker), adding name mangling for overloading/namespaces, the One Definition Rule (ODR), and C++20 Modules to replace brittle textual #include headers.

Headers, Include Guards & ODR

C++20 Modules

Modules compile an interface unit (.ixx / .cppm) once into a binary module interface, eliminating header re-parsing and macro leakage:

// math.cppm
export module math;

export int add(int a, int b) {
    return a + b;
}
// main.cpp
import math;

int main() {
    return add(2, 3);
}

Name Mangling & extern "C" ABI Boundaries

Because C++ supports function overloading and namespaces, compilers encode parameter types into linker symbol names (e.g., _Z3addii on Itanium ABI). When exposing a C++ library to C, Python (ctypes), Rust, or Java (FFM/JNI), wrap the boundary functions in extern "C" and catch all exceptions before crossing the ABI:

extern "C" int add_c_abi(int a, int b) noexcept {
    return a + b;
}
# Clang / GCC (C++23, strict warnings)
clang++ -std=c++23 -O2 -Wall -Wextra -Wpedantic -Wconversion main.cpp -o app

Linked from

Related notes