Makefile Tutorial

Learn GNU Make from first rule to real projects: targets and prerequisites, variables, pattern rules, functions, task runners, multi-directory builds, and parallel builds.

GNU Make rebuilds only what changed. It reads a Makefile describing how files depend on each other and runs the minimum set of commands to bring everything up to date. That idea powers C and C++ builds, the Linux kernel, and countless Python, Go, JavaScript, Docker and Terraform projects that use a Makefile as their command menu.

This path explains rules, targets and prerequisites from scratch, then covers how Make expands variables, pattern rules and automatic variables, built-in functions and conditionals. You will use Make as a self-documenting task runner, organise multi-directory projects, generate header dependencies automatically, run safe parallel builds, and finish with complete Makefiles for real projects. Every Makefile was run with GNU Make 4.3.

What You'll Learn

  • Getting Started
  • Core Concepts
  • Advanced Features
  • Real-World Projects

11 tutorials · Beginner to advanced · Exercises with solutions in every article · Free, no sign-up

Start here

Introduction to Make

What GNU Make is, how it uses a dependency graph and file timestamps to rebuild only what changed, and how it compares with CMake, Ninja, just and Task.

Section 1. Getting Started

Start here to learn the Makefile basics.

  • —Introduction to Make – What GNU Make is, how it uses a dependency graph and file timestamps to rebuild only what changed, and how it compares with CMake, Ninja, just and Task.
  • —Installing GNU Make – Install GNU Make on Linux, macOS and Windows, understand why macOS still ships Make 3.81, and set up your editor so recipe lines keep their TAB characters.
  • —Your First Makefile: Rules, Targets, and Prerequisites – Write a Makefile from scratch for a multi-file C program: rule anatomy, the TAB rule, timestamp rebuilds, object files, phony targets, recipe prefixes and dry runs.

Section 2. Core Concepts

  • —Variables and How Make Expands Them – How GNU Make variables really work: recursive versus simple expansion, command-line and environment overrides, the conventional CC/CFLAGS family, target-specific values, define and export.
  • —Pattern Rules and Automatic Variables – Replace repetitive rules with % pattern rules, read automatic variables like $@, $< and $^, understand Make's built-in rules, and build into separate output directories.
  • —Built-in Functions – GNU Make's built-in functions for text, file names, loops, conditions, shell commands and debugging, applied to discovering sources and generating per-module build rules.

Section 3. Advanced Features

Section 4. Real-World Projects

  • —Real-World Makefiles – Four complete, tested Makefiles to adapt for your own projects: a C program with tests and install, a Python task runner, a data pipeline, and Terraform and Docker wrappers.