Nexus Learn

Lecture 6: Compiler Linker Loader

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

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. The Question
  3. 2. The Four Stages
  4. 3. Seeing Every Stage Yourself
  5. 4. Four Kinds of Errors
  6. 5. Word Bank (Key Terms)
  7. 6. Self-Check Questions

UNIVERSITY OF AGRICULTURE, FAISALABAD

Department of Computer Science

LECTURE 6 · STUDENT NOTES

From Source Code to Running Program: Preprocessor, Compiler, Linker & Loader

CS-307 Programming Fundamentals · BSCS Semester 1

Week

Slides

Linked practical

Reference ID

3

Lecture6_Compiler_Linker_Loader.pptx

Practical 4

N06

Learning Outcomes

SLO

By the end of this lecture you can…

Builds CLO

Bloom level

SLO 6.1

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

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

C2

SLO 6.2

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

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

C1

SLO 6.3

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.

C3

How to use these notes

Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste.

1. The Question

The CPU only understands machine code (Lecture 5). We write C++ text. So how does hello.cpp become something the CPU can run? In Lecture 1 we saw a short answer: write → compile → link → run. Today we look inside each step. (Slide L06-S05)

File

What it is

Size (our test)

hello.cpp

our source code (11 lines)

307 bytes

hello.ii

after the preprocessor (36,593 lines!)

907,818 bytes

hello.o

machine code, not runnable yet

2,440 bytes

hello

final executable (hello.exe on Windows)

16,544 bytes

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

Real-world picture: a Faisalabad garment factory

Cutting room – collect all the cloth pieces and patterns → preprocessor

Stitching – sew the pieces into a shirt, reject bad seams → compiler

Finishing – add buttons and labels made by other suppliers → linker

Delivery – put the shirt on the shop shelf so it can be used → loader

2. The Four Stages

2.1 The build pipeline

(Slide L06-S06)

Figure 6.1 – The build pipeline

Stage

Input

Output

Main job

1 Preprocessor

hello.cpp

hello.ii

Handles # lines: pastes #include files, replaces #define names, removes comments

2 Compiler

hello.ii

hello.s → hello.o

Checks syntax; translates to assembly and then machine code (object file)

3 Linker

hello.o + library code

hello.exe

Joins object files and library code; fills in calls such as cout

4 Loader (OS)

hello.exe

program in RAM

Copies the program into memory and starts it at main()

Build = preprocess + compile + link. Run = load + execute.

2.2 Stage 1: the preprocessor

The preprocessor is a text editor that runs automatically before the compiler. It works only on lines that start with # (called directives). It does not check whether your C++ is correct. (Slide L06-S07)

hello.cpp

#include <iostream>

#define PASS_MARK 50 // the preprocessor replaces PASS_MARK with 50

using namespace std;

int main() {

// this comment is removed by the preprocessor

int marks = 72;

cout << "Pass mark is " << PASS_MARK << endl;

cout << "Your marks: " << marks << endl;

return 0;

}

End of hello.ii (made with g++ -E) — PASS_MARK became 50, the comment is gone

... 36,000+ lines from iostream ...

int main() {

int marks = 72;

cout << "Pass mark is " << 50 << endl;

cout << "Your marks: " << marks << endl;

return 0;

}

Directive

What the preprocessor does

#include <iostream>

pastes the whole iostream header file (and the files it includes) into ours — that is why hello.ii is so big

#define PASS_MARK 50

replaces every PASS_MARK in the code with 50

// comment

removed

Directives do not end with a semicolon, because they are not C++ statements.

2.3 Stage 2: the compiler

The compiler has two jobs. (Slide L06-S08)

1. Check the rules (syntax). A missing ;, an undeclared variable or a missing bracket is a compile error. No object file is made until all are fixed.

2. Translate. C++ → assembly language (hello.s, readable text) → machine code in an object file (hello.o / hello.obj).

Why can't we run hello.o?

The object file still has holes: it says "call cout here" but does not contain the code of cout. The linker fills these holes.

2.4 Stage 3: the linker

The linker joins one or more object files with ready-made library code (such as the code behind cout) and makes one executable file. Large programs are split into several .cpp files; each is compiled separately and the linker joins them. (Slide L06-S09)

Figure 6.2 – The linker joins object files and library code

Building a program from two files

g++ main.cpp area.cpp -o app

If something was promised (declared) but its code is never found, the linker stops with undefined reference — a link error.

2.5 Stage 4: the loader

The loader is part of the operating system. When you double-click the .exe or press Run, it: (Slide L06-S10)

1. reads the executable from the disk;

2. copies its instructions and data into RAM;

3. gives it memory for variables (this is why &marks changes on every run — Lecture 5);

4. sets the Program Counter to the first instruction of main();

5. then the CPU starts the fetch–decode–execute cycle.

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

3. Seeing Every Stage Yourself

3.1 g++ commands

(Slide L06-S11) You will run these in Practical 4.

Stage

Command

Output file

Preprocess only

g++ -E hello.cpp -o hello.ii

hello.ii (text, huge)

Compile to assembly

g++ -S hello.cpp -o hello.s

hello.s (text)

Compile to object

g++ -c hello.cpp -o hello.o

hello.o (binary)

Link

g++ hello.o -o hello

hello / hello.exe

All at once

g++ hello.cpp -o hello

hello / hello.exe

Output of the finished program

Pass mark is 50

Your marks: 72

3.2 What the IDE does

(Slide L06-S12)

Dev-C++ button

Stages it runs

Where messages appear

Compile (F9)

preprocess → compile → link

Compiler / Build log tab

Run (F10)

load → execute

Console window

Compile & Run (F11)

all of them

both

Reading error messages

Fix the first message first; one mistake can cause many messages.

Messages start with file:line:column: err_compile.cpp:6:5 means line 6, column 5.

4. Four Kinds of Errors

4.1 Overview

(Slide L06-S13)

Kind

Found by

When

Example

Compile (syntax) error

preprocessor / compiler

while building

missing ; · #include <iostrem>

Link error

linker

end of building

function declared but never written; main misspelled

Run-time error

the running program crashes

while running

integer division by 0

Logic error

only you, by testing

after running

a + b + c / 3 for an average

4.2 Compile errors

(Slide L06-S14) The missing ; is on line 5, but the compiler reports line 6 — it only notices when it reaches the next statement.

err_compile.cpp

#include <iostream>

using namespace std;

int main() {

int marks = 72

cout << marks << endl;

return 0;

}

g++ says

err_compile.cpp: In function 'int main()':

err_compile.cpp:6:5: error: expected ',' or ';' before 'cout'

6 | cout << marks << endl;

| ^~~~

A wrongly spelled header is found even earlier, by the preprocessor:

g++ says (for #include <iostrem>)

err_pre.cpp:1:10: fatal error: iostrem: No such file or directory

1 | #include <iostrem>

| ^~~~~~~~~

compilation terminated.

4.3 Link errors

The code below compiles without error (g++ -c succeeds), because the declaration promises that area() exists. The linker then cannot find it. (Slide L06-S14)

err_link.cpp

#include <iostream>

using namespace std;

double area(double r); // promise: this function exists somewhere

int main() {

cout << area(2.0) << endl;

return 0;

}

Linker (ld) says

/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

Clues for a link error: the words undefined reference, and ld (the linker) instead of a line number in your file.

4.4 Run-time errors

The program builds and works for some inputs but crashes for others. (Slide L06-S15)

err_run.cpp

#include <iostream>

using namespace std;

int main() {

int total = 500, students;

cout << "How many students? ";

cin >> students;

cout << "Share: " << total / students << endl;

return 0;

}

Two runs (Linux)

How many students? 4

Share: 125

How many students? 0

Floating point exception

Integer division by zero stops the program. On Windows (Dev-C++) the console closes and shows a large return value, for example 3221225620, instead of this message.

4.5 Logic errors

err_logic.cpp

#include <iostream>

using namespace std;

int main() {

int a = 70, b = 80, c = 90;

double avg = a + b + c / 3; // wrong: only c is divided

cout << "Average: " << avg << endl;

return 0;

}

Output (the correct average is 80)

Average: 180

No tool complains; only testing with values whose answer you already know shows the mistake. Fix: (a + b + c) / 3.0.

5. Word Bank (Key Terms)

Word

Simple meaning

Source code

The C++ text you write (.cpp)

Preprocessor

Tool that handles # lines before compiling

Directive

A line starting with #, e.g. #include

Compiler

Checks syntax and translates source code to machine code

Assembly language

Low-level text form of machine instructions

Object file

Machine code of one .cpp file, not yet runnable (.o / .obj)

Linker

Joins object files and library code into an executable

Library

Ready-made code you can use, e.g. iostream

Executable

A file the computer can run (.exe on Windows)

Loader

Part of the OS that copies a program into RAM and starts it

Syntax

Grammar rules of a programming language

Undefined reference

Linker message: something declared was never found

Run-time error

A crash while the program is running

Logic error

The program runs but the answer is wrong

6. Self-Check Questions

1. Name the four stages in order and the output file of each.

2. What does #include <iostream> do? Why is the .ii file so large?

3. Why can an object file not be run directly?

4. What is the job of the linker? When does it report 'undefined reference'?

5. What does the loader do when you run a program?

6. Classify: (a) missing ; (b) main spelt mian (c) 10 / n with n = 0 (d) area = 3.14 * r * 2.

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

Answers are in §2.1, §2.2, §2.3, §2.4, §2.5, §4.1 and §4.5. Homework is on Slide L06-S19.

← All CS-307 material