Linker: What is it and how it works? Fixing build errors step by step
Understand how the linker combines files into a ready program. Learn to fix 'undefined reference' errors and discover linking differences.
Linker: What is it and how it works?
Imagine you are building furniture from a flat pack. The compiler is the factory that produced the screws and cut the boards (object files). However, those parts lying on the floor are not a cabinet yet. You need a Linker – the assembly worker who takes the instructions, connects the boards with the right screws, and ensures the whole thing stands stable. Without a linker, your code is just a collection of unrelated fragments.
Linker
How does the linker work? Symbol Resolution
The linker no longer reads your source code. It operates on so-called symbols (names of functions and variables). Its job is to "stitch together" the places where you call a function with the place where that function is actually defined.
Static vs Dynamic Linking
How the linker attaches libraries to your program affects whether the user will need to install extra files (like the famous "DLL missing" errors).
| Cecha | Feature | Static Linking |
|---|---|---|
| File size | Large (everything inside) | Small (uses external files) |
| Portability | Very easy (single file) | Harder (requires .dll / .so files) |
| Performance | Slightly higher | Startup overhead |
Most Common Errors: Why won't the program build?
Linker errors appear at the very end of the build process. They usually mean the linker is "missing parts" for your puzzle.
Error: Undefined Reference
Error: Multiple Definition
Practice: How to fix the error in the terminal?
If you see an "undefined reference" message, you probably forgot to add one of the files to the compilation command:
Focus on adding the missing module:
1gcc main.o -o app
1gcc main.o engine.o -o app
Pro Tip: Correct use of -L and -l flags
Watch your flag syntax!
Quiz: Check your knowledge
What happens if a program using dynamic linking cannot find a required .dll file?
What next?
- Learn about Include Guards and how they protect against multiple definition errors.
- Check out Makefiles – they automate the linker's work in large projects.
- Explore the nm tool, which allows you to peek at symbols hidden inside object files.
- Try building a project with the
-staticflag to see how your.exefile size changes.
You might also like
Understanding the #include Directive in C++
A guide to the #include directive: learn how the preprocessor links files and why the choice of brackets matters.
Programming Libraries: Don't Reinvent the Wheel
Learn what programming libraries are and how to use them. A comparison of static and dynamic (DLL) libraries for beginners.
C-Style Strings: Char Arrays and Null Termination
Understanding low-level text handling: how character arrays terminated by a null byte work.