Linkage
Linkage is the rule that decides whether two declarations of the same name refer to the same object or function. C has external linkage, internal linkage, and no linkage.
Detailed Explanation
The compiler sees one translation unit at a time. The linker sees the object files. Linkage is the agreement between them about names.
External linkage means the name is a global symbol. int count; at file scope, void draw(void) { }, and anything declared extern without static participate. Two files that both define int count; with external linkage collide. Two files that both declare extern int count; and one file that defines int count = 0; share one object. This is how a library exports an API.
Internal linkage means the name is private to the translation unit. static int count; at file scope and static void helper(void) produce symbols the linker will not use to satisfy other files. Two files may each have a static void helper(void) with different bodies. Use this for every function that is not part of your header.
No linkage means the name is a local. A variable inside a function, even a static local, does not participate in linking. Two functions may each have an int n. A static local still has static duration; it just has no linkage.
const objects at file scope have external linkage in C, unlike C++. const int max = 10; in a header included by two files is two definitions of the same external symbol, and the linker will object. Write static const int max = 10; or enum { MAX = 10 }; in the header, or extern const int max; plus one definition.
The inline keyword interacts with linkage in a way that has burned people since C99. An inline function definition in a header is not, by itself, an external definition. One translation unit must emit a stand-alone copy, usually with extern inline or a non-inline definition. Many projects avoid the issue by using static inline in headers.
Name collisions are not only a link error. A static function that should have been exported will produce undefined reference at the call site in another file. An accidentally exported helper will collide with a third-party helper. Prefix exported names, and keep everything else static.
Code Examples
#include <stdio.h>
/* Internal linkage: another .c file may also define helper(). */
static int helper(void)
{
return 7;
}
int public_api(void); /* external linkage, defined here */
int public_api(void)
{
return helper();
}
int main(void)
{
printf("%d\n", public_api()); /* prints: 7 */
return 0;
}#include <stdio.h>
/* Safe header-style constant: internal linkage per translation unit. */
static const int max_retries = 3;
int main(void)
{
printf("%d\n", max_retries); /* prints: 3 */
return 0;
}Common Gotchas
File-scope `const int x = 1;` in a header has external linkage in C
include it twice and the linker sees two definitions. Use `static const` or `enum`.
Forgetting `static` on a helper exports it
the name becomes part of the global namespace and can collide at link time with a library you have not read.
A `static` local has no linkage and static duration
the keyword is doing duration work, not hiding a symbol from the linker.
`inline` in C is not `static inline`
without a single external definition, some toolchains report missing symbols at link time, others only when the function is not inlined.