Loading...

C essentialsModular C

Build automation with Make

12 min lesson

Next
  • Make is a build tool that runs the commands needed to turn source files into an executable, recompiling only what changed.
  • It matters because typing long gcc commands by hand is slow and error-prone, and large projects have many files and dependencies.
  • Think of Make as a recipe book where each dish (target) lists its ingredients (dependencies) and the steps (commands) to make it.
  • A Makefile contains rules in the form target: dependencies, followed by a tab-indented command line.
  • A target is usually a file to build, like app or main.o; dependencies are the files it needs; the command is what to run.
  • Make checks timestamps: if any dependency is newer than the target, it runs the command; otherwise it skips it.
  • The computer compares file modification times, so only out-of-date targets are rebuilt, which makes builds fast.
  • The syntax is: app: main.o math_utils.o followed by a tab and gcc main.o math_utils.o -o app.
  • A realistic use: rebuilding only the .o file whose .c changed, then relinking the executable.
  • A surprising detail: command lines in a Makefile must start with a real tab character, not spaces, or Make reports 'missing separator'.
  • The example Makefile builds a small program from two source files and includes a clean target.
Keep in mind
  • Write rules as target: dependencies followed by a tab-indented command.
  • Let Make decide what to rebuild based on file timestamps.
  • Add a clean target to remove build outputs easily.
  • Use variables like CC and CFLAGS to keep the Makefile short and consistent.
  • Automating builds is a professional habit. Great work!
Makefile
# Makefile for a small two-file C program
CC = gcc
CFLAGS = -Wall -Wextra -std=c11

# Default target: build the app
app: main.o math_utils.o
	$(CC) $(CFLAGS) main.o math_utils.o -o app

# Compile main.c into main.o
main.o: main.c math_utils.h
	$(CC) $(CFLAGS) -c main.c

# Compile math_utils.c into math_utils.o
math_utils.o: math_utils.c math_utils.h
	$(CC) $(CFLAGS) -c math_utils.c

# Remove build outputs
clean:
	rm -f app main.o math_utils.o

Breaking it down

Variables

CC = gcc

Defines a name you can reuse with $(CC). It keeps the Makefile short and makes it easy to switch compilers.

Default target

app: main.o math_utils.o

The first rule is the default when you run make. It depends on the two object files.

Link command

$(CC) $(CFLAGS) main.o math_utils.o -o app

Runs after both object files exist and are up to date. The tab at the start is required.

Object rule

main.o: main.c math_utils.h

Rebuilds main.o when main.c or the header changes. The header is listed because a change there can affect main.o.

clean target

clean: rm -f app main.o math_utils.o

Removes the build outputs. Run with make clean to start fresh.

Makefile essentials

ConceptMeaningExample
TargetFile to buildapp
DependencyFile needed firstmain.o
CommandHow to build itgcc ... -o app
VariableReusable value$(CC)
cleanRemove outputsmake clean

A helpful analogy

A Makefile is like a recipe book. Each recipe (target) lists the ingredients (dependencies) and the steps (commands) to make it. The cook (Make) checks what is already fresh and only cooks the dishes that are missing or stale.

Nesting rules

  • 1.Command lines must start with a real tab character, not spaces.
  • 2.The first rule in the file is the default target when you run make.
  • 3.List headers as dependencies when a source file includes them, so changes trigger rebuilds.
  • 4.Use variables like CC and CFLAGS so commands stay short and consistent.

Common mistakes

Using spaces instead of a tab before commands

Avoid

app: gcc main.c -o app

Use instead

app: gcc main.c -o app

Make requires a tab to recognize a command line. Spaces cause a 'missing separator' error.

Forgetting to list headers as dependencies

Avoid

main.o: main.c

Use instead

main.o: main.c math_utils.h

If the header changes, main.o is not rebuilt and the program may use outdated declarations. List headers that the .c includes.

Naming a target with the same name as an existing file

Avoid

clean: # but a file named clean exists

Use instead

.PHONY: clean clean:

Make compares timestamps. If a file named clean exists and is newer, the clean target is skipped. Mark it .PHONY.

Quick tips

  • •Run make to build and make clean to remove outputs.
  • •Add .PHONY: all clean so these targets always run.
  • •Use $(CC) and $(CFLAGS) instead of typing the compiler everywhere.
  • •Try touching math_utils.h and running make to see what rebuilds.
  • •Read make's output; it prints each command it runs.
Previous
Next