extern and Linkage
Share symbols across files.
What Is Linkage?
Linkage describes whether a name can be shared between different source files.
A name with external linkage refers to the same entity across the whole program. A name with internal linkage is private to its own file. Understanding linkage is key to multi-file programs.
Sharing a Variable
Suppose two files need access to one global counter. You cannot simply define it in both files; that creates two separate variables and a duplicate-symbol link error.
Instead, you define it once and declare it everywhere else with extern.
/* config.c */
int g_count = 0; /* the one definition */All lessons in this course
- Header and Source Files
- Include Guards
- extern and Linkage
- Compiling Multiple Files