← Back to Home

Makefile Basics

2026.07.23 · Linux · 2 min read

When I first started learning Makefiles, I thought of them as a kind of instruction manual — they tell the computer: which file you want, where that file comes from, and which command produces it.

The core idea comes down to a single sentence:

Which file depends on what, and which command produces the target file.

A Complete Makefile

This is the Makefile I wrote while practicing my Process Manager (pshow):

CC = gcc
CFLAGS = -Wall -Wextra -O2
TARGET = pshow

all: $(TARGET)

$(TARGET): main.c
	$(CC) $(CFLAGS) -o $(TARGET) main.c

clean:
	rm -f $(TARGET)

.PHONY: all clean

Breaking It Down

Variables

CC = gcc
CFLAGS = -Wall -Wextra -O2
TARGET = pshow

Set the compiler and its flags as variables, then reference them later with $(variable_name). The benefit: if you switch compilers or tune the flags later, you only change one line.

Targets and Dependencies

$(TARGET): main.c
	$(CC) $(CFLAGS) -o $(TARGET) main.c

$(TARGET) (that is, pshow) depends on main.c. If main.c changes, running make again recompiles it automatically.

The character in front of the second line is a Tab, not spaces — that's a Makefile syntax requirement.

.PHONY

.PHONY: all clean

Tells make that all and clean are command names, not actual files. Without this line, if the directory happened to contain a file named clean, make would assume that target is already done and skip it.

Compiler Flags

FlagDescription
-WallShow basic warnings
-WextraShow more warnings
-O2Optimization level 2
-oSpecify the output filename

Common Commands

make         # build (runs all)
make clean   # remove generated files

Key Takeaways