External Linkage
The extern
keyword
is used to declare that the symbol is external to current source file.
Imagine source1.cpp
:
int value = 100;
Imagine source2.cpp
void foo () {
extern int value; // (1)!
value++; // (2)!
}
- Find the
value
variable outside ofsource2.cpp
. - Uses the
value
variable.
These can be defined within the same source file too.
int value = 100;
void foo () {
// extern int value;
value++;
}
The extern
declaration in this case becomes optional.