C++ Tips : Translation Unit
Translation unit, internal linkage, external linkage.
C++ Tips : Translation Unit
My C++ Notes
Translation Unit
Translation Unit
- Consist of SOURCE file after it has processed by PREPROCESSOR.
- Header files are literally included.
- All macros are expanded and conditional compilation is resolved.
- Each translation unit is compiled independently.
External Linkage
- function or global variable is accessible throughout your program
Internal Linkage
- ONLY accessible in 1 translation unit
You can explicitly control the linkage by using extern
and static
keyword. Default linkage:
- non-const -> extern
- const -> intern
1
2
3
4
5
6
7
8
9
10
11
// global scope.
int i; // extern
extern const int j; // explicitly extern
const int x; // intern
static int y; // explicitly intern
int f(); // extern
static int sf(); // explicitly intern
It is better to use unnamed namespace
for making explicitly intern.
Stack overflow: What is external linkage and internal linkage?.
This post is licensed under CC BY 4.0 by the author.