Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Translation units, linkage, and the ODR

Translation units let parts of a program be translated separately. Linkage determines name identity across declarations, while the ODR constrains the number and form of definitions. Distinguishing these concepts helps you split code correctly between headers and implementation files.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

extern int counter; // declaration
int counter = 0; // definition
static int local_counter = 0; // namespace-scope internal linkage
namespace { int private_counter = 0; }

Translation unit

In a typical non-module program, a translation unit is a processed source file together with content included through #include, after conditional compilation. A header is therefore not automatically a separate unit. Units can be translated separately and then linked.

Linkage versus scope

Scope determines where a name can be found; linkage determines whether declarations can denote the same entity. External linkage allows declarations across units to correspond. Internal linkage confines them to one unit; namespace-scope static and unnamed namespaces provide it, among other cases. Module linkage concerns declarations attached to the same named module. An ordinary local variable has no linkage. Linkage does not determine object lifetime.

ODR — one-definition rule

The declaration extern int counter; does not define an object; int counter = 0; does. One unit cannot define the same definable item repeatedly. A function or variable used in a definition-requiring way (odr-use) outside a discarded statement must have a definition. An ordinary non-template, non-inline function or variable cannot have multiple definitions in the program. Class types, templates, and inline entities can have definitions across units only under ODR conditions, including matching tokens and name lookup and no attachment to a named module. Not every violation requires a diagnostic.

Example and limitations

counter.cpp and main.cpp form two units. Both declare the same counter, but only the former defines it; step remains internal. The header guard prevents repeated header content within one unit, not duplicate definitions across units. The sources are an evolving standard draft, not frozen C++23 text; the example targets local verification with the C++23 flag, without attesting a production runtime.

Reference identifiers covered by this entry: ODR / linkage / translation unit.

Examples

// counter.hpp
#ifndef COUNTER_HPP
#define COUNTER_HPP
extern int counter;
int next();
#endif

// counter.cpp
#include "counter.hpp"
namespace { int step = 2; }
int counter = 0;
int next() { return counter += step; }

// main.cpp
#include "counter.hpp"
#include <iostream>
int main() {
    std::cout << next() << '\n';
    std::cout << counter << '\n';
}

Result

2
2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources