Course contentsShow
← C
Lesson 28 of 1,2191. FoundationsFree lesson

Phase 4: The Linker

Phase 4: The Linker

What you'll learn: Understand how the final stage of compilation stitches separate machine code files and libraries into a single, runnable program.

The IKEA Analogy

Imagine you are building a house. The Compiler is like a factory that creates specific components: one factory makes the windows, another makes the doors, and a third makes the roof. However, at the end of that process, you don't have a house; you just have a pile of parts sitting in separate boxes (these are your .obj or .o files).

The Linker is the construction crew that arrives at the site. Its job is to look at the blueprints, grab the doors, fit them into the frames, and bolt everything together. If a door is missing, or if two factories both sent the "front door," the crew can’t finish the job.

Resolving References

In C, we often use functions that we didn't write ourselves, such as printf. When your code is translated into machine code during the assembly phase, the computer sees a "placeholder" for printf. It knows it needs to run that function, but it doesn't know where the actual instructions for printf live.

The Linker searches through Standard Libraries (pre-written collections of code) to find the implementation of printf. It then "links" your code to that library code.

#include <stdio.h>

// The compiler sees this and knows how it should look.
// The linker finds the actual machine code for printf 
// and connects it to your main function.
int main() {
    printf("Hello, world!");
    return 0;
}

Creating the Executable

Once the Linker has gathered all your custom object files and all the necessary library files, it performs a final check. It ensures that every function called has exactly one definition. If you try to use a function that doesn't exist, you get a Linker Error (like the famous undefined reference).

If everything matches up, the Linker spits out a single file: the Executable (like a.out on Linux or program.exe on Windows). This is the only file you actually "run."

Key takeaway

The Linker is the final assembly line that resolves external references and merges separate machine code files into one standalone executable.