C++ Symbol Resolution

6 min

Learn how the linker resolves symbols and fixes undefined references with interactive visualizations.

Best viewed on desktop for optimal interactive experience

Symbol Resolution

Symbol resolution is the linker's process of matching undefined symbols (like function calls) with their definitions across object files.

Symbol Resolution

Symbol Table

Resolution Process

Select a symbol to see resolution process
Successful Resolution

All symbols found and linked correctly

Symbol Types

Strong Symbols

Initialized globals and functions:

int x = 10; // Strong void func() { } // Strong

Weak Symbols

Uninitialized globals:

int y; // Weak __attribute__((weak)) int z = 5; // Explicitly weak

Undefined Symbols

External references:

extern int external_var; // Undefined void external_func(); // Undefined

Resolution Rules

  1. Multiple strong symbols → Error
  2. One strong, multiple weak → Choose strong
  3. Multiple weak symbols → Choose any
  4. No definition found → Undefined reference error

Name Mangling

C++ encodes function signatures for overloading:

void func(int) // _Z4funci void func(double) // _Z4funcd void func(int, int) // _Z4funcii // Prevent mangling with C linkage extern "C" void c_func();

Common Errors & Solutions

Undefined Reference

undefined reference to `function()'

Solutions:

  • Ensure function is implemented
  • Link the correct library
  • Check name mangling (C vs C++)

Multiple Definition

multiple definition of `variable'

Solutions:

  • Use extern in headers
  • Use inline for C++17
  • Use anonymous namespaces

Viewing Symbols

# List symbols nm object.o # Demangle C++ symbols nm object.o | c++filt # Undefined symbols only nm -u object.o

Next Steps

If you found this explanation helpful, consider sharing it with others.

Mastodon