On this page
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
- Every
.cppfile plus all headers it#includes forms one translation unit (TU). - Guard headers with
#pragma once(or classic#ifndefguards). - One Definition Rule (ODR): Non-inline functions and variables can have many declarations across TUs, but exactly one definition in the linked program. Templates and
inlinefunctions/variables (inline constexpr int kMax = 100;) may appear identically in multiple TUs.
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;
}
Recommended Baseline Compiler Flags
# Clang / GCC (C++23, strict warnings)
clang++ -std=c++23 -O2 -Wall -Wextra -Wpedantic -Wconversion main.cpp -o app
Linked from
Related notes
- tuts/cpp/builtins/namespaces Linked here; links to this page; shared tags: #cpp, #prog, #tier
- tuts/cpp/language_and_raii/templates_concepts_and_polymorphism Linked here; links to this page; shared tags: #cpp, #prog, #tier
- tuts/cpp/language_and_raii Linked here; links to this page; shared tags: #cpp, #prog
- tuts/programming/memory_and_execution/compilation_interpretation_and_jit Linked here; links to this page; shared tags: #prog
- tuts/cpp/tutorials Linked here; shared tags: #cpp, #prog
- tuts/c/compilation_and_internals Linked here; shared tags: #prog
- tuts/cpp/builtins/auto shared tags: #cpp, #prog, #tier
- tuts/cpp/builtins/c_style_casts shared tags: #cpp, #prog, #tier
- tuts/cpp/builtins/casts shared tags: #cpp, #prog, #tier
- tuts/cpp/builtins/classes_and_virtual shared tags: #cpp, #prog, #tier