The Compilation Process
Overview
This week introduces the compilation pipeline that turns source code into an executable program.
Starting with a simple Hello World in C/C++, we’ll explore each step: preprocessing, compiling, assembling, and linking. Students will gain hands-on experience using gcc or g++ at each stage and learn how Makefiles automate the build process.
By the end of this week, you should be able to manually trace the build process, understand the role of each stage, and create a simple Makefile for structured project builds.
Key Concepts
- Source code lifecycle: from
.cor.cppto executable binary - Stages of the compilation pipeline:
- Preprocessing (
cpp): handling#include,#define, macros, conditional compilation - Compilation: converting preprocessed code into assembly
- Assembling: turning assembly into object files (
.o) - Linking: combining object files and libraries into an executable
- Preprocessing (
- Using
gccorg++to invoke each stage individually - Understanding object files and executables with
file,nm,objdump - Static vs. dynamic linking basics
- Build automation: Makefiles, rules, targets, dependencies
- Other useful tools:
make,pkg-config,ldd
Practice / Lab
- Hello World Compilation Steps:
- Run
gcc -E hello.c -o hello.i(preprocessor) - Run
gcc -S hello.i -o hello.s(compiler → assembly) - Run
gcc -c hello.s -o hello.o(assembler → object file) - Run
gcc hello.o -o hello(linker → executable)
- Run
- Exploring Artifacts:
- Inspect
.i,.s,.oand executable withlessandfile - Use
nm hello.oto list symbols - Use
objdump -d hello.oto view assembly
- Inspect
- Makefile Basics:
- Write a simple Makefile with targets:
all,clean - Practice
make,make clean, dependency tracking
- Write a simple Makefile with targets:
- Linking Check:
- Use
ldd helloto see linked libraries - Compare static vs dynamic builds (
gcc -static)
- Use
Homework
Guides
References & Resources
Required
- GNU Compiler Collection (GCC) Documentation
- Make Manual (GNU Make)
- Introduction to the Compilation Process (GeeksforGeeks)
- ECE 252 Lecture 1: Our C Toolkit
- Understanding C and C++ compilation process
- C++: препроцессор, компилятор, компоновщик (RUS)
- The C++ compilation model
- Memory Layout of C Program
Optional
- The Linux Programming Interface (Kerrisk) – Chapter 4: Program Execution
- Learn Makefiles by Example (GNU)
- objdump, nm, and ldd Usage Guide (Tutorialspoint)
- Compiling with g++
- C Programming: Makefiles
- Begineer Makefile tutorial
- Makefile tutorial
- 9 Essential GNU binutils tools
- CMake Tutorial EP 1 | Understanding The Basics
Tools
gcc,cpp,as,ldmake,pkg-config,ldd,objdump,nm
Quiz (Self-check)
- What is the role of the preprocessor in compilation?
- What file extension is typically produced by the assembler?
- How does static linking differ from dynamic linking?
- Which tool is used to inspect the symbols inside an object file?
- Why are Makefiles useful in larger projects?