Nexus Learn

Practical 4: Inside the Build: Preprocess, Compile, Link & Run

Lab Practical CS-307 Programming Fundamentals 8 min read Updated 8 Nov 2026
On this page
  1. 1. Lab Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A: Warm-up — Run the Four Stages (4 min)
  6. Part B: Predict the Kind of Error (4 min)
  7. Part C: Bug Hunt (5 min)
  8. Part D: Graded Lab Tasks (30 min · 10 marks)
  9. Part E: Post-Lab Questions (finish as homework if needed)
  10. Marking Rubric (10 marks)
  11. Submission

Duration

Language

Tools

Linked Lecture

50 minutes

C++

g++ (MinGW, comes with Dev-C++/Code::Blocks) + Command Prompt; Dev-C++ project for Task 2

Lectures 5 and 6

Mapped outcomes: CLO4 (C++ syntax and semantics) at psychomotor level P3 – Guided Response · PLO2, PLO5

1. Lab Objectives

By the end of this 50-minute lab, you will be able to:

  • Run each build stage on its own (g++ -E, -c, link) and name the file it makes.
  • Tell a compile error, a link error, a run-time error and a logic error apart from what you see.
  • Use sizeof and & to see how variables are stored in memory.
  • Build a program from two .cpp files and fix the link error that appears when one is missing.

2. Word Bank

Word

Simple meaning

Real-world example

Preprocessor

Handles # lines before compiling

Collecting all cloth pieces before stitching

Object file (.o)

Machine code of one .cpp file, not runnable yet

A shirt without buttons

Linker

Joins object files and library code

The finishing unit that adds buttons

Executable (.exe)

The final program you can run

The finished shirt on the shop shelf

Undefined reference

Linker could not find something that was promised

Buttons ordered but never delivered

sizeof / &

Size in bytes / address in memory

Size of a plot / its house number

3. Background

Build = preprocess → compile → link; Run = the loader copies the .exe into RAM and the CPU executes it (Lectures 5 and 6). Dev-C++ does all of this with one click. Today you run the stages one by one from the Command Prompt.

Opening a Command Prompt in your folder (Windows)

Open your lab folder in File Explorer, click the address bar, type cmd and press Enter.

Type g++ --version. If you see a version number, g++ is ready. If not, ask the instructor (the MinGW bin folder must be on the PATH).

No g++ on your PC? Use an online compiler for Parts B–D and skip the -E/-c commands in Part A.

4. Time Plan (50 minutes)

Part

Activity

Time

—

Briefing

3 min

A

Warm-up: run the four stages

4 min

B

Predict the kind of error

4 min

C

Bug hunt (3 bugs)

5 min

D

Graded tasks 1–3

30 min

E

Post-lab questions & submission

4 min

Part A: Warm-up — Run the Four Stages (4 min)

Type and save this program as lab4_stages.cpp.

lab4_stages.cpp

#include <iostream>

#define RATE 280 // rupees per US dollar (example rate)

using namespace std;

int main() {

// convert dollars to rupees

double dollars = 25;

cout << dollars << " USD = " << dollars * RATE << " PKR" << endl;

return 0;

}

Run these commands one by one and fill the table.

Commands

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

g++ -c lab4_stages.cpp -o lab4_stages.o

g++ lab4_stages.o -o lab4_stages

lab4_stages

Command

Stage(s)

File made

Can you read it in Notepad?

g++ -E …

g++ -c …

g++ lab4_stages.o …

lab4_stages

Open lab4_stages.ii, scroll to the very end and find main(). What happened to RATE and to the comment?

My observation

Part B: Predict the Kind of Error (4 min)

For each program, write your prediction (compile / link / run-time / logic / no error) before building it. Then build and run it (type 0 when b3 asks for input).

b1.cpp

#include <iostream>

using namespace std;

int main() {

int x = 10

cout << x << endl;

return 0;

}

b2.cpp

#include <iostream>

using namespace std;

void greet();

int main() {

greet();

return 0;

}

b3.cpp

#include <iostream>

using namespace std;

int main() {

int sweets = 20, children;

cout << "Children: ";

cin >> children;

cout << "Each gets " << sweets / children << endl;

return 0;

}

b4.cpp

#include <iostream>

using namespace std;

int main() {

double r = 5;

cout << "Area: " << 3.14 * r * 2 << endl;

return 0;

}

Program

My prediction

What really happened

b1.cpp

b2.cpp

b3.cpp

b4.cpp

Part C: Bug Hunt (5 min)

Each program has one bug. Build it, write the message (or wrong output), say which kind of error it is, and fix it.

bugC1.cpp

#include <iostram>

using namespace std;

int main() {

cout << "Welcome to the UAF lab" << endl;

return 0;

}

Message / wrong output, kind of error, fix

bugC2.cpp

#include <iostream>

using namespace std;

int main() {

int price = 350, qty = 4;

int total = price * qty;

cout << "Total: Rs. " << totl << endl;

return 0;

}

Message / wrong output, kind of error, fix

bugC3.cpp

#include <iostream>

using namespace std;

int main() {

double celsius = 40;

double fahrenheit = 9 / 5 * celsius + 32;

cout << celsius << " C = " << fahrenheit << " F" << endl;

return 0;

}

Message / wrong output, kind of error, fix

Part D: Graded Lab Tasks (30 min · 10 marks)

Rules

Save each task as lab4_task1.cpp, lab4_task2 (folder), lab4_task3.cpp.

Test with the sample input and one input of your own.

Your addresses will be different from the sample — that is normal.

Task 1: Memory Map of a Student Record (Easy — 3 marks)

Declare five variables for one student: roll number (int), CGPA (double), section (char), hostelite (bool) and CNIC (long long). Print a table showing each variable's name, value, size in bytes and address, then the total number of bytes. Use setw and left (Lecture 3) to line up the columns.

Hint for the char address

cout << &section tries to print text, because a char address is treated like a string. Write (void*)&section to print the address as a number.

Sample run (addresses will differ)

Variable Value Bytes Address

roll 2026101 4 0x7ffeb1e14e94

cgpa 3.62 8 0x7ffeb1e14e98

section B 1 0x7ffeb1e14e92

hostelite 1 1 0x7ffeb1e14e93

cnic 3310012345671 8 0x7ffeb1e14ea0

Total bytes: 22

Task 2: A Program in Two Files (Medium — 3 marks)

A farmer wants the area of his field and of a round water tank on it. The instructor gives you area.cpp with two ready-made functions. You write main.cpp: declare the two functions at the top (as shown), input the tank radius and the field's length and width, and print both areas and the area left.

area.cpp (given — do not change)

// area.cpp — ready-made functions (given by the instructor; do not change)

double circleArea(double r) {

return 3.14159 * r * r;

}

double rectArea(double length, double width) {

return length * width;

}

Put these two lines above main() in main.cpp

double circleArea(double r); // promises: these functions

double rectArea(double length, double width); // exist in another file

Build three ways and record what happens:

1. g++ main.cpp area.cpp -o farm then run farm.

2. g++ main.cpp -o farm2 (area.cpp left out).

3. g++ -c main.cpp, g++ -c area.cpp, then g++ main.o area.o -o farm3.

4. Dev-C++: File → New → Project → Console Application; add both files to the project; press F11.

Sample run

Water tank radius (ft): 7

Field length (ft): 150

Field width (ft): 90

Tank area : 153.938 sq ft

Field area : 13500 sq ft

Field area left after the tank: 13346.1 sq ft

Message from build 2 and which stage gave it

Task 3: University Storage Calculator (Challenge — 4 marks)

UAF keeps one record per student: roll (int), CGPA (double), grade (char) and fee paid (bool). Input the number of students. Using sizeof (not fixed numbers), print the bytes per record, the total bytes, the total in KB and MB (1 KB = 1024 bytes, 1 MB = 1024 KB, 2 decimals), and how many records would fit in 8 GB of RAM.

Hints

Use long long for big whole numbers (8 GB is more than an int can hold).

Divide by 1024.0, not 1024, to keep the decimals (Lecture 3).

8 GB in bytes: 8LL * 1024 * 1024 * 1024.

Sample run

Number of students: 25000

Bytes per record : 14

Total bytes : 350000

Total KB : 341.80

Total MB : 0.33

Records that fit in 8 GB RAM: 613566756

Part E: Post-Lab Questions (finish as homework if needed)

Q1. In Task 2, why did main.cpp compile but the build fail when area.cpp was left out?

Answer

Q2. bugC3 gave no error message at all. Which kind of error is it, and how can you find such errors?

Answer

Q3. Why is lab4_stages.ii tens of thousands of lines long when lab4_stages.cpp has only 11 lines?

Answer

Marking Rubric (10 marks)

Component

Marks

What earns full marks

Task 1

3

Five variables of the right types; value, sizeof and address for each; aligned table; total bytes.

Task 2

3

Two-file program builds and runs; link error from build 2 recorded and explained.

Task 3

4

sizeof used; long long for big numbers; KB/MB with 2 decimals; 8 GB capacity correct.

Parts A–C and the post-lab questions are checked in the viva. A program that does not build gets at most half marks for that task.

Submission

  • Show your running programs to the instructor before leaving.
  • Copy the final code and output of each task, and the Part A table, into your Lab Book (Practical 4 pages).
  • Copying code from a classmate gets zero marks for both students.

Next practical (same week): Practical 5 — From Algorithm & Flowchart to Code (top-down design and trace tables, Lecture 7).

← All CS-307 material