Skip to content
System Programming
Compiling C/C++

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 .c or .cpp to 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
  • Using gcc or g++ 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)
  • Exploring Artifacts:
    • Inspect .i, .s, .o and executable with less and file
    • Use nm hello.o to list symbols
    • Use objdump -d hello.o to view assembly
  • Makefile Basics:
    • Write a simple Makefile with targets: all, clean
    • Practice make, make clean, dependency tracking
  • Linking Check:
    • Use ldd hello to see linked libraries
    • Compare static vs dynamic builds (gcc -static)

Homework


Guides

References & Resources

Required

Optional

Tools

  • gcc, cpp, as, ld
  • make, pkg-config, ldd, objdump, nm

Quiz (Self-check)

  1. What is the role of the preprocessor in compilation?
  2. What file extension is typically produced by the assembler?
  3. How does static linking differ from dynamic linking?
  4. Which tool is used to inspect the symbols inside an object file?
  5. Why are Makefiles useful in larger projects?