Nexus Learn

Lecture 6: Compiler Linker Loader

Lecture Slides CS-307 Programming Fundamentals 8 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Slide 2

Quick Recap of Lecture 5

5 minutes · homework check

Remember

  • Program AND data live in memory (stored program)
  • CPU = Control Unit + ALU + registers
  • Fetch → decode → execute, again and again
  • PC holds the address of the next instruction
  • sizeof → bytes; &x → address

Today's question: the CPU only understands machine code — so how does our C++ text become machine code?

Recap

Programming Fundamentals · Lecture 6

Slide 3

Today's Plan

50 minutes · 8 parts

Recap & homework check

5 min

Learning outcomes & a surprise

3 min

The four stages: preprocess, compile, link, load

15 min

Seeing every stage and its file

7 min

Four kinds of errors

9 min

Class activity: Which stage complains?

7 min

Common mistakes & recap quiz

3 min

Homework & next lecture

1 min

Programming Fundamentals · Lecture 6

Slide 4

Learning Outcomes (SLOs)

By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)

Describe the four stages that turn a .cpp file into a running program: preprocessor, compiler, linker, loader

SLO 6.1 → CLO4 · C2

Name the input and output file of each stage (.cpp → .ii → .o → .exe)

SLO 6.2 → CLO4 · C1

SLO 6.3 → CLO4 · C3

Classify an error as a compile, link or run-time error and fix it

CLO4 Describe the syntax and semantics of the C++ programming language.

Programming Fundamentals · Lecture 6

Slide 5

A Surprise: 11 Lines Became 36,593 Lines

What happens between pressing Build and seeing the output?

  • Real-world: a Faisalabad garment factory
  • 1 Cutting room: collect all the cloth pieces and patterns → preprocessor
  • 2 Stitching: sew pieces into a shirt; reject bad seams → compiler
  • 3 Finishing: add buttons and labels from other suppliers → linker
  • 4 Delivery: put the shirt on the shop shelf so it can be sold → loader

Measured with g++ on a 64-bit Linux PC. Your numbers will be a little different, but the pattern is the same.

Module 1 · The Four Stages

Programming Fundamentals · Lecture 6

Slide 6

The Build Pipeline

Each stage takes one file in and gives one file out

  • hello.cpp
  • source code
  • PREPROCESSOR
  • #include, #define
  • hello.ii
  • expanded source
  • COMPILER
  • check + translate
  • hello.o
  • object code
  • LINKER
  • join + libraries
  • hello.exe
  • executable
  • iostream library
  • (ready-made code)
  • LOADER (OS)
  • copies .exe into RAM

Build = preprocess + compile + link (one click in Dev-C++).

Run = load into RAM + CPU starts the fetch–decode–execute cycle at main().

Module 1 · The Four Stages

Programming Fundamentals · Lecture 6

Slide 7

Stage 1 · The Preprocessor

Works only on lines that start with # · it edits text, it does not check C++

hello.cpp (before)

hello.ii (after) — g++ -E

  • #include <iostream>
  • #define PASS_MARK 50
  • using namespace std;
  • int main() {
  • // this comment is removed
  • int marks = 72;
  • cout << "Pass mark is " << PASS_MARK;
  • ... 36,000+ lines of iostream ...
  • int main() {
  • int marks = 72;
  • cout << "Pass mark is " << 50;

Module 1 · The Four Stages

Programming Fundamentals · Lecture 6

Slide 8

Stage 2 · The Compiler

Checks the grammar (syntax), then translates to machine code

  • Job 1: check the rules
  • Missing ; undeclared variable wrong type missing bracket
  • → compile error, no .o file is made

hello.s — first lines

  • .file "hello.cpp"
  • .text
  • #APP
  • ... (88 lines of assembly)
  • Job 2: translate
  • C++ → assembly (g++ -S, hello.s)
  • assembly → machine code in an object file (g++ -c, hello.o)
  • The .o file still has 'holes' for cout.
  • Real-world
  • A translator turns an Urdu letter into English, line by line, and refuses to continue if a sentence makes no sense.

Module 1 · The Four Stages

Programming Fundamentals · Lecture 6

Slide 9

Stage 3 · The Linker

Joins object files and library code into one executable

  • main.o
  • (needs area())
  • Real-world
  • Finishing unit in the factory: buttons and labels come from other suppliers and are attached here.
  • Missing buttons = shirt cannot be sold = link error.
  • area.o
  • (has area())

LINKER

  • app.exe
  • ready to run
  • C++ library
  • (has cout)

Command for two files: g++ main.cpp area.cpp -o app

Module 1 · The Four Stages

Programming Fundamentals · Lecture 6

Slide 10

Stage 4 · The Loader

Part of the operating system (Windows, Linux, Android)

Reads hello.exe from the disk (storage)

  • Real-world
  • Delivery van takes the finished shirts from the warehouse (disk) to the shop shelf (RAM), where customers (the CPU) can use them.

Copies its instructions and data into RAM

Gives it memory for variables (this is why &marks changes every run!)

Sets the PC to the first instruction of main()

CPU starts fetch → decode → execute (Lecture 5)

Compile once, run many times: the .exe can be loaded again and again without compiling.

Module 1 · The Four Stages

Programming Fundamentals · Lecture 6

Slide 11

Seeing Every Stage Yourself with g++

You will do this in Practical 4

Output of ./hello

  • Pass mark is 50
  • Your marks: 72

On Windows the executable is hello.exe; the object file may be hello.o or hello.obj.

Module 2 · Seeing Every Stage

Programming Fundamentals · Lecture 6

Slide 12

What Your IDE Does When You Click

Dev-C++ and Code::Blocks run the same stages for you

  • Tip: read the FIRST error message first
  • One small mistake can cause many messages. Fix the first one, build again, and many of the others disappear.
  • The message starts with file:line:column — for example err_compile.cpp:6:5 means line 6, column 5.

Module 2 · Seeing Every Stage

Programming Fundamentals · Lecture 6

Slide 13

Four Kinds of Errors

Which stage finds the problem tells you where to look

Real-world: a spelling mistake (compile), a missing page (link), a page that tears while reading (run-time), a wrong fact that reads fine (logic).

Module 3 · Four Kinds of Errors

Programming Fundamentals · Lecture 6

Slide 14

Compile Error vs Link Error

Real messages from g++

err_compile.cpp (line 5 has no ;)

err_link.cpp (area is never written)

  • int main() {
  • int marks = 72
  • cout << marks << endl;
  • double area(double r); // promise only
  • int main() {
  • cout << area(2.0) << endl;

Compiler says

Linker says

  • err_compile.cpp: In function 'int main()':
  • err_compile.cpp:6:5: error: expected
  • ',' or ';' before 'cout'
  • /usr/bin/ld: err_link.o: in function `main':
  • err_link.cpp:(.text+0x15): undefined
  • reference to `area(double)'
  • collect2: error: ld returned 1 exit status

Module 3 · Four Kinds of Errors

Programming Fundamentals · Lecture 6

Slide 15

Run-Time Error vs Logic Error

Both programs build without any error

err_run.cpp

err_logic.cpp

  • int total = 500, students;
  • cin >> students;
  • cout << "Share: "
  • << total / students << endl;
  • int a = 70, b = 80, c = 90;
  • double avg = a + b + c / 3;
  • cout << "Average: " << avg;

Two runs

Output (should be 80!)

  • How many students? 4
  • Share: 125
  • How many students? 0
  • Floating point exception

Average: 180

Fix: (a + b + c) / 3.0 → 80

Module 3 · Four Kinds of Errors

Programming Fundamentals · Lecture 6

Slide 16

CLASS ACTIVITY · 7 MIN · IN PAIRS

Which Stage Complains?

For each mistake write: compile error, link error, run-time error or logic error — and the stage that finds it.

Mistakes

Hint

  • 1. #include <iostrem>
  • 2. cout << "Hi" (no ; at the end)
  • 3. Function main written as mian
  • 4. Area of a circle as 3.14 * r * 2
  • 5. int x = 10 / n; when the user types 0
  • 6. Using variable total without declaring it
  • Did the program build?
  • → No: compile or link.
  • Did it crash while running?
  • → run-time.
  • Did it run but give a wrong answer?
  • → logic.

Slide 17

Common Mistakes

Clear these up now

✗

"The compiler runs my program."

✓ The compiler only translates. The OS loader + CPU run it.

✗

"#include is a C++ statement."

✓ It is a preprocessor directive: no ; and it is handled before compiling.

"undefined reference = a spelling mistake in my code line."

✓ It is a LINK error: something declared was never defined or linked.

✗

"0 errors, 0 warnings = my program is correct."

✓ Logic errors are invisible to the compiler. Test it!

✗

Programming Fundamentals · Lecture 6

Slide 18

Quick Recap

Answer aloud

Q1

Name the four stages in order and the file each one produces.

Q2

What does #include <iostream> do, and which stage handles it?

Q3

"undefined reference to `area(double)'" — which kind of error is this?

Q4

Why is a logic error harder to find than a compile error?

Programming Fundamentals · Lecture 6

Slide 19

Homework

Draw the build pipeline: hello.cpp → … → running program. Write the tool and the output file for every arrow.

Write one small program for EACH of the four kinds of error. Build/run each and copy the exact message you get.

Explain in 3–4 lines why the .ii file is so much bigger than the .cpp file.

Differentiate between a compiler and a linker (three points).

Next lecture: Problem-solving techniques — top-down design, stepwise refinement and dry runs

← All CS-307 material