Quiz Entry - updated: 2026.07.14
What happens if you define the same global variable in multiple files?
It depends on initialization: two initialized definitions = link error; one initialized beats an uninitialized one; multiple uninitialized are an error under -fno-common (GCC 10+ default) but silently merged otherwise.
| Scenario | Result |
|---|---|
| Multiple initialized definitions | Linker error (multiple strong symbols) |
| One initialized + uninitialized | Initialized wins (strong beats weak) |
| Multiple uninitialized (with -fcommon) | Linker picks one (dangerous!) |
| Multiple uninitialized (with -fno-common) | Linker error (default since GCC 10) |
Example of dangerous behavior:
// file1.c
// Uninitialized (weak if -fcommon)
int x;
// file2.c
// Different type, same name!
double x;
// Linker might silently merge these - disaster!
Safe practices:
- Always initialize global variables
- Use
staticfor file-private globals - Use
externin headers, define in one .c file - Compile with
-fno-common(default in GCC 10+) - Use
-Werrorto catch warnings
Tip: The -fno-common flag turns uninitialized globals into strong symbols, catching conflicts at link time.
Go deeper:
Weak symbol (Wikipedia) — how strong overrides weak, and why duplicate strong definitions collide.