Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Packages and Services

Linux make command

Reference for the Linux make command: targets, prerequisites, recipes, variables, phony targets, pattern rules and options such as -j, -n and -C, with examples.

make runs recipes to bring targets up to date. You describe, in a file called Makefile, which files depend on which others and which commands produce them. When you run make, it compares timestamps and runs only the recipes whose targets are older than their prerequisites. That makes it a classic build tool for C programs, and also a handy task runner for anything else.

A rule has three parts: a target, its prerequisites after the colon, and a recipe of shell commands on the following lines. Each recipe line must start with a tab character (unless you change .RECIPEPREFIX); spaces give the error "missing separator".

Terminal
make            # build the first target in the Makefile
make clean      # build a named target
make -j4 all    # run up to 4 recipes in parallel

Try it

Bash A first Makefile

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Variables, phony targets and a dry run

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
make [OPTION]... [VARIABLE=value]... [TARGET]...
MAKE
target: prerequisites
	recipe
Item Description
-f FILE Use FILE instead of GNUmakefile, makefile or Makefile.
-C DIR Change to DIR before reading the Makefile.
-j [N] Run N recipes at once (no number: as many as possible).
-n, --dry-run Print the commands without running them.
-k Keep going after errors in independent targets.
-B Rebuild everything, ignoring timestamps.
-s Silent: do not echo recipe lines.
-p Print the database of rules and variables (helps debugging).
VAR=value Override a variable from the command line.
$@, $<, $^, $* Automatic variables: the target, the first prerequisite, all prerequisites, the pattern stem.
.PHONY Declares targets that are names of actions, not files.
:= and = Set a variable immediately (:=) or lazily when used (=).

Returns: 0 when everything is up to date or built; 2 if a recipe fails or no rule is found. With -q it returns 1 when something is out of date.

Usage notes

  • Tabs matter. "*** missing separator. Stop." almost always means a recipe line starts with spaces, or a line is neither a rule nor an assignment.
  • Each recipe line runs in its own shell. cd dir on one line does not affect the next; join them with && or end lines with a backslash. A recipe line stops the build when its command fails, unless you prefix it with -.
  • Mark action targets .PHONY (all, clean, test, install). Without it, a file called clean would make make clean say "is up to date".
  • A $ in a recipe is a make variable; write $$ to pass a dollar sign to the shell ($$HOME, $$(date)).
  • The first target is the default, so put all first.
  • Echoed lines: prefix a recipe line with @ to hide the command itself.
  • Timestamps, not contents, decide what is rebuilt. Headers an object file includes must be listed as prerequisites (or generated with gcc -MMD), otherwise edits to them are ignored.
  • Parallel builds with -j need correct prerequisites; missing ones cause races that only appear sometimes.
  • Variables from the environment are visible, and command-line make CC=clang overrides assignments in the file.
  • Implicit rules: make hello with a hello.c and no Makefile compiles it with cc, using CFLAGS and LDFLAGS.