What You'll Learn
- How C++ name mangling works
- extern "C" for C interoperability
- The compile → link pipeline
- Static vs dynamic linking
The C++ ABI, Name Mangling & Linking
The ABI (Application Binary Interface) defines how compiled C++ code is laid out in memory — how functions are called, how names are encoded, and how object files are linked together. Understanding it explains why mixing compilers fails, why extern "C" exists, and how linking actually works.
Name Mangling — Why Overloading Works
C allows only one function per name. C++ supports overloading — multiple functions with the same name but different parameters. The compiler achieves this by mangling names: encoding parameter types into the symbol name so the linker can distinguish process(int) from process(double).
Pro Tip: Use c++filt to demangle symbols: c++filt _Z7processi outputs process(int). Essential for reading linker errors and debugging symbol resolution.
Name Mangling
See how overloaded functions get unique mangled names
#include <iostream>
using namespace std;
// C++ mangles function names to encode parameter types
// This enables function overloading at the linker level
void process(int x) {
cout << "process(int): " << x << endl;
}
void process(double x) {
cout << "process(double): " << x << endl;
}
void process(int x, int y) {
cout << "process(int, int): " << x << ", " << y << endl;
}
// Mangled names (GCC-style):
// process(int) → _Z7processi
// process(double) → _Z7processd
//
...extern "C" — Disabling Mangling
When you need C code to call C++ functions (or vice versa), name mangling breaks things — C doesn't understand mangled names. extern "C" tells the compiler to use C-style (unmangled) names for those functions.
Common Mistake: Using extern "C" with overloaded functions. Since C doesn't support overloading, all extern "C" functions must have unique names.
extern C
Disable mangling for C-compatible function names
#include <iostream>
using namespace std;
// extern "C" disables name mangling — essential for C interop
// Without it, C code can't find C++ functions (different names)
extern "C" {
int c_add(int a, int b) {
return a + b;
}
double c_multiply(double a, double b) {
return a * b;
}
}
// Common pattern: header that works for both C and C++
// #ifdef __cplusplus
// extern "C" {
// #endif
//
// int my_lib_init(void);
// void my_lib_cleanup(void);
//
// #ifdef __cplu
...The Build Pipeline & Linking
The C++ build has three stages: the preprocessor expands macros and includes, the compiler generates object files, and the linker combines object files into an executable. Static libraries (.a) are copied into the binary; dynamic libraries (.so/.dll) are loaded at runtime.
Build Pipeline
Understand compilation stages and linkage types
#include <iostream>
#include <string>
using namespace std;
// === The C++ Build Pipeline ===
// 1. Preprocessor: #include, #define, #ifdef → expanded source
// 2. Compiler: source → object files (.o / .obj)
// 3. Linker: object files → executable
// Each .cpp file compiles independently into an .o file
// The linker resolves symbols across all .o files
// === Static vs Dynamic Linking ===
// Static (.a / .lib): code copied into executable
// + Self-contained, no runtime depend
...Quick Reference
| Concept | Description |
|---|---|
| Name mangling | Encodes types into symbol names |
| extern "C" | Disables mangling for C interop |
| Static linking | Library code embedded in binary |
| Dynamic linking | Library loaded at runtime |
| c++filt | Demangle symbol names |
Lesson Complete!
You now understand name mangling, extern "C", and the compile-link pipeline that turns C++ source into executables.
Sign up for free to track which lessons you've completed and get learning reminders.