Course contentsShow
← C
Lesson 809 of 1,21911. The Preprocessor and BuildFree lesson

Symbol tables and visibility

Symbol tables and visibility

What you'll learn: How the linker uses a "phonebook" of names to connect your separate code files into a single working program.

The program's phonebook

Imagine you are building a house. You have one blueprint for the plumbing and another for the electrical wiring. If the plumbing blueprint says "Connect to the main power switch," the plumber needs to know exactly where that switch is located on the electrical blueprint.

In C, each source file (.c) is compiled into an object file (.o). During this process, the compiler creates a symbol table. Think of this as a phonebook for that specific file. It contains a list of every function and global variable name (the "symbols") defined in that file, along with their "address" or location.

Global vs. Local visibility

Not everything in your phonebook is meant to be shared. C uses two main levels of visibility:

  1. External Linkage (Global): By default, functions are public. Their names are added to the symbol table so the linker can see them from other files.
  2. Internal Linkage (Private): If you put the keyword static before a function or global variable, it becomes "unlisted." It exists in the file, but it is not added to the public symbol table. Other files cannot see or use it.

The Linker's job

The linker is the final coordinator. It takes all your .o files and looks for "missing" addresses. If main.c calls a function named calculate_total(), the compiler leaves a blank spot. The linker then scans the symbol tables of all other files to find a match.

If it finds calculate_total in math_utils.o, it "plugs in" the address. If it can't find it—or finds two things with the same name—it throws a linker error.

// File: math_utils.c

// This is "unlisted." Other files can't see it.
static int helper_math(int n) {
    return n * 2;
}

// This is public. It goes in the symbol table for the linker.
int calculate_total(int a, int b) {
    return helper_math(a + b);
}

How the linker sees it

When you build this program, the linker sees calculate_total as a valid destination it can point other files toward. However, if another file tries to call helper_math, the linker will fail because that symbol was marked private (static) and kept out of the global phonebook.

Key takeaway

A symbol table is a map of names used by the linker to connect code across different files, controlled by visibility keywords like static.