wleci
AboutProjectsBlogContact
Contact
Back to blog
ProgrammingDev ToolsC / C++

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.

December 24, 20254 min read
Share:

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

A utility program that combines object files (.o / .obj) generated by the compiler and libraries into a single final executable file (e.g., .exe or .out).

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).

CechaFeatureStatic Linking
File sizeLarge (everything inside)Small (uses external files)
PortabilityVery easy (single file)Harder (requires .dll / .so files)
PerformanceSlightly higherStartup 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

The most common issue. The compiler believed you that the function exists (because it saw the declaration in a .h file), but the Linker couldn't find its code in any of the provided .c / .cpp files.

Error: Multiple Definition

You defined the same function or variable in two different files. The Linker doesn't know which version to use in the final program.

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:

Terminal
$gcc main.o -o app
/usr/bin/ld: main.o: undefined reference to 'calculate_tax' # Linker says: I don't know what 'calculate_tax' is!

Focus on adding the missing module:

Before
1gcc main.o -o app
After
1gcc main.o engine.o -o app

Pro Tip: Correct use of -L and -l flags

Watch your flag syntax!

In GCC, the `-L` flag points to the folder where the linker should search for libraries, and the `-l` flag points to the library name. **Important:** For a library named `libmath.a`, include it as `-lmath` (remove 'lib' and '.a'). Always place library flags at the end of the command!

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 -static flag to see how your .exe file size changes.

You might also like

INF.04Programming

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.

3 min read
INF.04Programming

Relational and Logical Operators: How to Compare Data?

Understand how comparison operators work and how to combine conditions using AND, OR, and NOT. Essential knowledge for control statements.

2 min read
INF.04Programming

Conditional Operator: Short-hand if-else in one line

Learn the ternary operator. Find out how to shorten your code and when to use it instead of classic if-else statements.

2 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland