Fundamentals 7 min read

Why Can Duplicate Global Variables Compile? Understanding Strong and Weak Symbols in C

This article explains why C programs can sometimes compile and run correctly even when global variables or functions are defined multiple times, by introducing symbol tables, the distinction between strong and weak symbols, and the linker rules that decide which definition is used.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why Can Duplicate Global Variables Compile? Understanding Strong and Weak Symbols in C

Background

When linking object files into an executable, the linker uses a symbol table that records every function and global variable defined in each file. Symbols are classified as strong (default for functions and initialized globals) or weak (default for uninitialized globals or those explicitly marked with __attribute__((weak))).

Example 1: Duplicate Function Definition

#include <stdio.h>
void func() {
    printf("编程珠玑
");
}

// main.c
#include <stdio.h>
void func() {
    printf("公众号
");
}
int main(void) {
    func();
    return 0;
}

Compiling fun.c and main.c together produces a linker error because func is defined twice as a strong symbol.

Example 2: Strong vs Weak Global Variables

// var.c
int num;               // uninitialized → weak
void change() {
    num = 1023;
}

// main.c
#include <stdio.h>
void change();
int num = 1024;        // initialized → strong
int main(void) {
    printf("before:num is %d
", num);
    change();
    printf("after:num is %d
", num);
    return 0;
}

Output:

before:num is 1024
after:num is 1023

The strong definition in main.c overrides the weak one from var.c, allowing the program to link and run correctly.

Symbol Inspection

// symbol.c
#include <stdio.h>
int symbol = 1024;
int func_symbol() {
    return 0;
}

Running nm symbol.o yields:

0000000000000000 T func_symbol
0000000000000000 D symbol
T

marks a function (text section) and D marks a global data symbol.

Strong and Weak Symbol Rules

Strong symbols cannot be duplicated.

If both strong and weak symbols with the same name exist, the strong one is selected.

If only weak symbols exist, the linker arbitrarily picks one of them.

Example 3: Multiple Weak Symbols

// var.c
int weak;                     // uninitialized → weak
int strong = 1024;            // initialized → strong
__attribute__((weak)) int weak1 = 2222; // explicitly weak
int main(void) {
    printf("编程珠玑
");
    return 0;
}

This code demonstrates how initialization and the weak attribute affect symbol classification.

Type Mismatch Pitfall

// var.c (different type)
double num;                  // weak, type double
void change() { num = 1023; }

When linked with the strong definition int num = 1024; from main.c, the program prints:

before:num is 1024
after:num is 0

The mismatch occurs because a double occupies 8 bytes while an int occupies 4 bytes, leading to truncation of the stored value.

Conclusion

Avoid giving global variables identical names unless you deliberately rely on strong/weak symbol behavior. Prefer limited‑scope variables or namespaces (e.g., C++ namespaces) to prevent subtle bugs. Strong and weak symbols are useful for building extensible libraries, but they must be used with care.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Compilationglobal variablesLinkersymbolsstrong-weak
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.