Project 3: The C vs. C++ Name Mangling Showdown
A tiny C++ library with overloaded functions, functions in a namespace, and a class method. You will then compile it and use command-line tools to inspect the object file’s symbol table, documenting the “mangled” names. Finally, you’ll add
extern "C"and see the difference.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | C++ |
| Alternative Languages | C |
| Difficulty | Level 1: Beginner |
| Time Estimate | A few hours |
| Knowledge Area | Linking / Compilers |
| Tooling | nm, objdump, or dumpbin (on Windows) |
| Prerequisites | Basic C++. |
What You Will Build
A tiny C++ library with overloaded functions, functions in a namespace, and a class method. You will then compile it and use command-line tools to inspect the object file’s symbol table, documenting the “mangled” names. Finally, you’ll add extern "C" and see the difference.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Using linker tools → maps to learning to use
nmorobjdump -t - Decoding mangled names → maps to recognizing patterns, or using
c++filtto demangle them - Understanding
extern "C"→ maps to seeing it as a directive that changes the “language linkage” of a function
Key Concepts
- Name Mangling: Wikipedia provides a good overview.
- The
nmcommand:man nm. extern "C": Any C++ book’s section on interoperability with C.
Real-World Outcome
namespace my_app {
int do_work(int x) { return x; }
int do_work(double y) { return (int)y; }
}
// This one will be C-compatible
extern "C" int callable_from_c(int a) {
return a + 1;
}
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
LEARN_C_ABI_DEEP_DIVE.md - “A Tour of C++” by Bjarne Stroustrup