01 / 21

C++ Translation Units

Understand how header files and source files combine into translation units: the fundamental building blocks of the C++ compilation process.

C++

What Is a Translation Unit?

A translation unit is the basic unit of compilation in C++. It consists of a single source file (.cpp) together with all the headers and other files it includes, after the preprocessor has finished expanding every #include directive.

Header vs Source Files

Header Files (.h / .hpp)
Contain declarations: function prototypes, class definitions, constants, and type aliases. Shared across multiple source files via #include.
Source Files (.cpp)
Contain definitions: function bodies, variable initializations, and the actual logic. Each one becomes its own translation unit.

The Preprocessor Builds It

Before the compiler analyzes any C++ syntax, the preprocessor runs first. It processes directives that start with # and produces the final text that becomes the translation unit.

1
#include: Copy-pastes the entire contents of the named file into the current position.
2
#define / #ifdef: Defines macros and conditionally includes or excludes blocks of code.
3
#pragma once / include guards: Prevents the same header from being pasted more than once into a single translation unit.

The Compilation Pipeline

main.cpp math.cpp math.h Preprocessor TU 1 TU 2 Compiler .o / .obj files Linker Executable

The One Definition Rule (ODR)

The One Definition Rule is the core law governing translation units. It has two parts:

1
Within one translation unit: every variable, function, class, or enum can be defined at most once.
2
Across the entire program: non-inline functions and non-inline variables must be defined in exactly one translation unit.

Include Guards Prevent Duplication

#pragma once
Modern, concise. Compiler remembers it already included this file and skips it on repeat.
#ifndef / #define / #endif
Traditional macro guard. Uses a unique macro name so the preprocessor skips the file contents on second include.
// math.h (traditional guard) #ifndef MATH_H #define MATH_H int add(int a, int b); #endif

Declaration vs Definition

Declaration Definition
Purpose Tells compiler a name exists Provides the full implementation
Where Header file (.h) Source file (.cpp)
Repetition Can appear in many TUs Must appear in exactly one TU
Example int add(int, int); int add(int a, int b) { return a+b; }

ODR Exceptions: What Can Go in Headers

Some things are allowed (or even required) to appear in multiple translation units, as long as every copy is identical:

inline functions
Marked inline explicitly, or defined inside a class body. Linker merges duplicates.
Class definitions
Struct/class bodies must be in headers so every TU that uses them knows the layout.
Templates
Template definitions must be visible at the point of instantiation, so they live in headers.

How to Organize a C++ Project

1
Create header files (.h) for each module. Put declarations, class definitions, and constants here. Add an include guard.
2
Create source files (.cpp) for each module. Include the matching header, then write function bodies and variable definitions.
3
#include only what you need. Each source file includes the headers it directly depends on: no more, no less.
4
Compile each .cpp independently into an object file. The compiler processes one translation unit at a time.
5
Link all object files together. The linker resolves cross-TU references and produces the final executable.

Example: Two-File Project

math.h
#pragma once int add(int a, int b); // declaration only
math.cpp
#include "math.h" int add(int a, int b) { return a + b; // definition }
main.cpp
#include "math.h" #include <iostream> int main() { std::cout << add(3, 4) << std::endl; }

Tracing Translation Unit 1

When the compiler processes main.cpp, the preprocessor expands all #include directives. The resulting translation unit looks like this:

// ---- expanded from "math.h" ---- int add(int a, int b); // ---- expanded from <iostream> ---- // (thousands of lines of std library) // ---- original main.cpp code ---- int main() { std::cout << add(3, 4) << std::endl; }

Example: Multi-Module Project

player.h enemy.h utils.h player.cpp enemy.cpp main.cpp TU 1 TU 2 TU 3 Linker game.exe HEADERS SOURCES TRANSLATION UNITS

Internal vs External Linkage

Linkage determines whether a name defined in one translation unit can be referenced from another:

Internal Linkage External Linkage
Visibility Only in this TU Across all TUs
Keywords static, unnamed namespace extern (default for functions)
Use case Helper functions, file-local data Shared APIs, global state
ODR scope Each TU has its own copy Must follow ODR across all TUs

Common Mistakes with Translation Units

✕ Defining functions in headers
Putting function bodies in a .h file without marking them inline causes multiple-definition linker errors when two .cpp files include it.
✕ Forgetting include guards
Without #pragma once or #ifndef guards, including a header twice in one TU causes "redefinition" compiler errors.
✕ Circular includes
File A includes B, B includes A. Causes incomplete types and cascading errors. Use forward declarations to break the cycle.

Summary: What Goes Where?

Construct Header (.h) Source (.cpp)
Function declaration ✓
Function definition ✓
Class/struct definition ✓
Inline function body ✓
Template definition ✓
Global variable definition ✓
extern variable declaration ✓
#include guards ✓

Question 1 of 5

A translation unit in C++ is best described as:

Question 2 of 5

You define a non-inline function int compute(int x) in a header file that is included by three .cpp files. What happens?

Question 3 of 5

Which of the following is safe to place in a header file without causing ODR violations?

Question 4 of 5

What is the primary purpose of #pragma once (or #ifndef include guards)?

Question 5 of 5

A function marked static in a .cpp file has: