Translation units
A translation unit is a single source file after the preprocessor has pasted in all its headers and expanded its macros. It is the unit the compiler sees, and it sees nothing else in your project.
Detailed Explanation
The compiler has no idea your project exists. It is handed one file at a time and told to produce one object file. Everything it knows must arrive through that file — either written there directly or pulled in by #include. That expanded blob of code is the translation unit, usually shortened to TU.
This is the single most useful mental model for explaining C++ build errors. Your stats.cpp calls formatBytes() defined in format.cpp. The compiler working on stats.cpp never opens format.cpp. It only believes the function exists because a header declared it. If no header declared it, you get "was not declared in this scope", even though the function is right there in the next file over.
A consequence people underestimate: a header included by twenty source files is compiled twenty times. Include <algorithm> in a widely-used header and you have added its full parse cost to every TU in the build. This is why C++ compile times are what they are, and why reducing header dependencies is real engineering work rather than tidiness.
Each name in a TU has a linkage, which decides whether other TUs can refer to it.
- External linkage — the name is visible to the linker and other TUs can link against it. Non-
staticfunctions and namespace-scope variables get this by default. - Internal linkage — the name exists only in this TU. Two TUs can each have their own
retryCountwith no conflict, and neither can see the other's. - No linkage — local variables, which do not appear in the symbol table at all.
You request internal linkage with static at namespace scope, which is a different meaning of static from the one used on class members or local variables. The modern spelling is an anonymous namespace: namespace { ... }. Everything inside gets a unique internal name. Anonymous namespaces work for types too, which namespace-scope static does not, so prefer them for helpers you do not want to export.
Across the whole program, the one-definition rule applies. A variable or non-inline function with external linkage may be defined exactly once. Classes, templates, and inline functions may be defined once per TU, but every definition must be token-for-token identical — which is why you put them in a header and include it everywhere rather than retyping them.
That rule is why inline exists. Its original meaning, "please paste this function body at the call site", is now a hint compilers largely ignore in favour of their own analysis. Its actual guaranteed effect is to relax the ODR: an inline function or (since C++17) an inline variable may be defined in every TU that includes the header, and the linker collapses the copies into one. Defining a non-inline free function in a header and including it twice gives a duplicate symbol error; marking it inline fixes that.
C++20 modules replace this whole arrangement. A module is compiled once into a binary interface that importing TUs read directly, so macros do not leak across the boundary, include order stops mattering, and the same header is not reparsed per TU. Toolchain support is still uneven, so most codebases in 2026 remain header-based, but modules are the direction the language is going.
Code Examples
logging.cpp — two ways to keep a helper private to this translation unit.
// logging.cpp — two ways to keep a helper private to this translation unit.
#include <string>
static int failureCount = 0; // internal linkage: legacy spelling
namespace { // internal linkage: preferred, works for types too
struct Entry { std::string text; int severity; };
bool isNoisy(const Entry& e) { return e.severity < 2; }
}
void record(const std::string& text, int severity) {
Entry e{text, severity};
if (!isNoisy(e)) ++failureCount; // isNoisy and failureCount are invisible elsewhere
}config.hpp — included by many source files.
// config.hpp — included by many source files.
#pragma once
// Not inline: every including TU defines it -> duplicate symbol at link time.
// int maxRetries = 3;
inline int maxRetries = 3; // C++17 inline variable: one shared object
inline int backoffMs(int attempt) { // inline function: one definition after linking
return 100 * (1 << attempt); // 100, 200, 400, ...
}Common Gotchas
`static` at namespace scope means something different from `static` on a member
at namespace scope it means internal linkage; on a class member it means one shared instance; on a local variable it means the value persists between calls. Same keyword, three unrelated jobs.
A `static` or anonymous-namespace entity in a header is a per-TU copy
each including file gets its own separate variable. A counter defined that way in a header silently counts per file instead of per program.
ODR violations are undefined behaviour, not diagnosed
compiling the same class with different `#define`s in two TUs (different member sets, different padding) is ill-formed with no diagnostic required. The build succeeds and the program corrupts memory at run time. This is what mismatched build flags across a project actually cause.
Macros ignore all of this
the preprocessor runs before linkage exists, so a `#define` leaks into everything included after it. That leakage is one of the problems C++20 modules were designed to end.