LOGBOOK

HELP

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:

  1. Always initialize global variables
  2. Use static for file-private globals
  3. Use extern in headers, define in one .c file
  4. Compile with -fno-common (default in GCC 10+)
  5. Use -Werror to catch warnings

Tip: The -fno-common flag turns uninitialized globals into strong symbols, catching conflicts at link time.

Go deeper:

From Quiz: REVE1 / Program Execution | Updated: Jul 14, 2026