---
tag: YFFVV1
kind: tool
slug: make
name: Make
aliases: [make, GNU Make, gmake, POSIX make, makefile, Makefile]
area: tools-and-workflow/build-systems
level: core
summary: >-
  Make is the build tool that reads rules from a makefile and runs the recipe of each target that is older than its prerequisites. GNU Make is the most used version, and POSIX defines a portable core.
role: [build]
licence: gpl-3.0-or-later
repo: https://git.savannah.gnu.org/git/make.git
homepage: https://www.gnu.org/software/make/
history:
  year: 1975
  people: [Stuart Feldman, Richard Stallman, Roland McGrath, Paul D. Smith]
  note: Stuart Feldman wrote Make at Bell Labs, and his paper of 1979 says that it was in use on Unix systems from 1975. Richard Stallman and Roland McGrath wrote GNU Make, and Paul D. Smith has maintained it from version 3.76.
see_also: [write-a-makefile, build-system-concept, build-graph, incremental-builds, cmake, ninja, build-a-project]
sources:
  - url: https://www.gnu.org/software/make/manual/make.html
    title: GNU Make manual, edition 0.77 for GNU make version 4.4.1, 26 February 2023
    locator: 1 Overview of make; 2.1 What a Rule Looks Like; 2.2 A Simple Makefile; 3.2 What Name to Give Your Makefile; 4.6 Phony Targets; 5.4 Parallel Execution; 6.2 The Two Flavors of Variables; 6.5 Setting Variables; 6.10 Variables from the Environment; 9.2 Arguments to Specify the Goals; 9.5 Overriding Variables; 9.8 Summary of Options; 10.2 Catalogue of Built-In Rules; 10.5 Defining and Redefining Pattern Rules; 14 Features of GNU make
  - url: https://www.gnu.org/software/make/
    title: GNU Make, home page
    locator: the introduction; Licensing
  - url: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/make.html
    title: The Open Group Base Specifications Issue 8 (IEEE Std 1003.1-2024), make
    locator: SYNOPSIS; DESCRIPTION (the .POSIX special target); Target Rules (the characters % and "); Internal Macros ($^); CHANGE HISTORY, Issue 8
  - url: https://ninja-build.org/manual.html
    title: The Ninja build system manual
    locator: Introduction; Philosophical overview
  - paper: doi:10.1002/spe.4380090402
    title: Stuart I. Feldman, Make, a program for maintaining computer programs, Software, Practice and Experience 9(4), pages 255 to 265, 1979
    locator: the abstract
---

## What it is

Make is a [[build-system-concept|build tool]]. It reads a file called the makefile, which lists the files of a program, the files that each one is made from, and the commands that make it. The GNU Make manual says that the make utility "automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them."[^1] Make uses the makefile and "the last-modification times of the files to decide which of the files need to be updated".[^1]

Make is not limited to one language. The GNU Make home page says that the makefile gives the shell commands for each non-source file, and that these commands can run a compiler, a linker, `ar` or a document formatter.[^2] Many projects also use Make to run tests, to install a package or to start other build tools.

There are several versions of Make. POSIX defines the `make` utility, which "shall update files that are derived from other files".[^3] GNU Make implements POSIX make and adds many features to it.[^1] The GNU manual compares GNU Make with the versions of Make in BSD and System V Unix.[^1] This page uses GNU Make, and it tells which features are not in POSIX.

## Install and first use

Install GNU Make with the package manager of the system, and use `make --version` to see which version you have. The examples on this page ran with GNU Make 4.3 and GCC 13 on Ubuntu 24.04.

A makefile is a list of rules. A rule has this shape: a target, a colon, the prerequisites, and then the recipe on the lines below.[^1] The manual explains the three parts:

- "A target is usually the name of a file that is generated by a program".[^1]
- "A prerequisite is a file that is used as input to create the target."[^1]
- "A recipe is an action that make carries out."[^1]

Each recipe line must start with a tab character. The manual calls this "an obscurity that catches the unwary."[^1]

The run below writes a makefile with two rules, for `hello` and `hello.o`. The first `make` runs both recipes. The second `make` finds that `hello` is up to date. After `touch hello.c`, the source has a later modification time than `hello.o`, so Make runs both recipes again.

```bash run id=first-use
d=$(mktemp -d)
cd "$d"
cat >hello.c <<'END'
#include <stdio.h>
int main(void) { puts("hello"); return 0; }
END
printf 'hello: hello.o\n\tcc -o hello hello.o\nhello.o: hello.c\n\tcc -c hello.c\n' >Makefile
cat Makefile
echo "--- first make"
make 2>&1
./hello
echo "--- second make"
make 2>&1
echo "--- after touch hello.c"
touch hello.c
make 2>&1
cd /
rm -rf "${d:?}"
```

```output for=first-use
hello: hello.o
	cc -o hello hello.o
hello.o: hello.c
	cc -c hello.c
--- first make
cc -c hello.c
cc -o hello hello.o
hello
--- second make
make: 'hello' is up to date.
--- after touch hello.c
cc -c hello.c
cc -o hello hello.o
```

When you give no target on the command line, Make builds the default goal. "By default, the goal is the first target in the makefile (not counting targets that start with a period)."[^1] Make looks for the makefile under the names `GNUmakefile`, `makefile` and `Makefile`, in that order, and the manual recommends `Makefile`.[^1]

## Concepts of the tool

**Rules and the graph.** Each rule adds edges to a [[build-graph]]: from the target to each prerequisite. To update a target, Make first updates its prerequisites, and then it runs the recipe if the target does not exist or is older than one of its prerequisites. This test on modification times is how Make does [[incremental-builds]].

**Variables.** A variable holds text, and `$(name)` expands it. GNU Make has two main flavors of variable.[^1]

- A "recursively expanded" variable is set with `=`. Its value is stored as written, and the references in it are expanded each time the variable is used.[^1]
- A "simply expanded" variable is set with `:=` or `::=`. Its value is expanded once, when the variable is defined.[^1] The manual says that only the `::=` form is in POSIX, which added it in Issue 8.[^1]

The `?=` operator sets a variable only if it is not yet defined.[^1] Make also reads each environment variable as a make variable of the same name, but an assignment in the makefile or on the command line overrides the environment.[^1] An argument such as `CFLAGS=-O0` on the command line overrides "all ordinary assignments of the same variable in the makefile".[^1] The run below shows the two flavors and the three places that a value can come from.

```bash run id=variables
d=$(mktemp -d)
cd "$d"
cat >Makefile <<'END'
x = one
lazy = $(x)
now := $(x)
x = two
CFLAGS ?= -O2

show:
	@echo "lazy=$(lazy) now=$(now) CFLAGS=$(CFLAGS)"
END
make 2>&1
make CFLAGS=-O0 2>&1
CFLAGS=-g make 2>&1
cd /
rm -rf "${d:?}"
```

```output for=variables
lazy=two now=one CFLAGS=-O2
lazy=two now=one CFLAGS=-O0
lazy=two now=one CFLAGS=-g
```

`lazy` has the value `two`, because Make expands `$(x)` when it uses `lazy`, and `x` is `two` by then. `now` keeps `one`. In the last line, `CFLAGS` comes from the environment, so `?=` does not change it.

**Automatic variables.** In a recipe, `$@` is the name of the target, `$<` is the name of the first prerequisite, and `$^` is the names of all the prerequisites.[^1] These variables "have values computed afresh for each rule that is executed".[^1] They have values only in the recipe, not in the list of targets or prerequisites.[^1]

**Pattern rules.** A pattern rule has one `%` in the target. The `%` "matches any nonempty substring", and a `%` in a prerequisite "stands for the same stem".[^1] The rule `%.o: src/%.c` tells Make how to make any `.o` file from the `.c` file of the same name in `src`. POSIX does not have pattern rules: it says that the meaning of a target that contains `%` is implementation-defined.[^3] The GNU manual says that several versions of Make have pattern rules, and that it does not know who invented them first.[^1]

**Phony targets.** "A phony target is one that is not really the name of a file".[^1] Targets such as `clean` and `test` name actions. If a file called `clean` exists, Make thinks that the target is up to date and does not run the recipe. The special target `.PHONY` fixes this: "Once this is done, 'make clean' will run the recipe regardless of whether there is a file named clean."[^1]

The run below uses a pattern rule, automatic variables and a phony target. `make -n` prints the recipes that Make would run, but it does not run them.[^1]

```bash run id=pattern-rules
d=$(mktemp -d)
cd "$d"
mkdir src
cat >src/add.c <<'END'
int add(int a, int b) { return a + b; }
END
cat >src/main.c <<'END'
#include <stdio.h>
int add(int a, int b);
int main(void) { printf("2 + 3 = %d\n", add(2, 3)); return 0; }
END
cat >Makefile <<'END'
CC = cc
CFLAGS = -O2 -Wall
objs := main.o add.o

app: $(objs)
	$(CC) -o $@ $^

%.o: src/%.c
	$(CC) $(CFLAGS) -c -o $@ $<

.PHONY: clean
clean:
	rm -f app $(objs)
END
make 2>&1
./app
echo "--- make -n after touch src/add.c"
touch src/add.c
make -n 2>&1
echo "--- make clean with a file named clean"
touch clean
make clean 2>&1
cd /
rm -rf "${d:?}"
```

```output for=pattern-rules
cc -O2 -Wall -c -o main.o src/main.c
cc -O2 -Wall -c -o add.o src/add.c
cc -o app main.o add.o
2 + 3 = 5
--- make -n after touch src/add.c
cc -O2 -Wall -c -o add.o src/add.c
cc -o app main.o add.o
--- make clean with a file named clean
rm -f app main.o add.o
```

**Built-in rules.** Make has a catalogue of implicit rules. For example, "n.o is made automatically from n.c" with the recipe `$(CC) $(CPPFLAGS) $(CFLAGS) -c`, and a program `n` is linked from `n.o` with `$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)`.[^1] A makefile with only the line `hello: hello.o` can therefore build a C program. The option `-r` turns off the built-in rules.[^1]

```bash run id=builtin-rules
d=$(mktemp -d)
cd "$d"
cat >hello.c <<'END'
#include <stdio.h>
int main(void) { puts("hello"); return 0; }
END
echo 'hello: hello.o' >Makefile
echo "--- make -r"
make -r 2>&1
echo "exit status: $?"
echo "--- make"
make 2>&1
./hello
cd /
rm -rf "${d:?}"
```

```output for=builtin-rules
--- make -r
make: *** No rule to make target 'hello.o', needed by 'hello'.  Stop.
exit status: 2
--- make
cc    -c -o hello.o hello.c
cc   hello.o   -o hello
hello
```

## Common tasks

| Task | Command |
|---|---|
| Build the default goal | `make` |
| Build one target | `make test` |
| Print the recipes, but do not run them | `make -n`[^1] |
| Run up to 8 recipes at the same time | `make -j8`[^1] |
| Continue after an error, to see all the errors | `make -k`[^1] |
| Rebuild all targets | `make -B`[^1] |
| Use another makefile | `make -f other.mk`[^1] |
| Run in another directory | `make -C src`[^1] |
| Set a variable for one run | `make CFLAGS=-O0`[^1] |
| Ask if a target is up to date, with no output | `make -q`, which gives the exit status 0 if it is and 1 if it is not[^1] |

The `-j` option sets "the number of recipes (jobs) to run simultaneously", and with no number Make runs as many recipes at the same time as it can.[^1] A parallel build is correct only when the makefile lists all the prerequisites of each target. If a prerequisite is missing, the serial order can hide the error, and `-j` can then run a recipe before its input is ready.

## Configuration

Make has no configuration file of its own: the makefile is the configuration. Some settings change how Make reads it:

- The `.RECIPEPREFIX` variable sets the character that starts a recipe line, in place of the tab.[^1]
- The `include` directive reads other makefiles. Allowing more than one file in one `include` is a GNU extension.[^1]
- The `MAKEFLAGS` variable passes options to the `make` commands that a recipe runs.[^1]
- A makefile that starts with the special target `.POSIX` asks for the behavior that POSIX describes.[^3] A portable makefile must also not use the special targets that are reserved for implementations.[^3]

Issue 8 of POSIX, from 2024, added features that GNU Make already had. Its change history lists the `::=`, `?=` and `+=` assignments, `-include`, the `$^` macro, the `.PHONY` special target and the `-j maxjobs` option.[^3] In Issue 8, `$^` gives "the list of prerequisites for the current target, with any duplicates (except the first) removed."[^3] An older Make can lack these features, so a makefile that must run with many versions of Make uses only the features of the older issues.

## Alternatives

- [[cmake]] and Meson do not build the program themselves. They generate the build files for Make or for Ninja from a description at a higher level.
- [[ninja]] is a small build tool that runs a graph that another program generates. Its manual says that "Where other build systems are high-level languages, Ninja aims to be an assembler."[^4]
- Bazel, Maven, Gradle and the build commands of Go, Cargo and .NET know the language that they build, so the user does not write the compile commands.

## History and versions

Stuart Feldman wrote Make at Bell Labs. His paper of 1979 describes "a program that can keep track of the relationships between parts of a program, and issue the commands needed to make the parts consistent after changes are made".[^5] The paper also says that "Make has been in use on UNIX systems since 1975."[^5]

Richard Stallman and Roland McGrath wrote GNU Make. Paul D. Smith has handled its development from version 3.76.[^1] GNU Make conforms to section 6.2 of POSIX.2.[^1] It is free software under version 3 or later of the GNU General Public License.[^2] The GNU manual takes the Make of 4.2 BSD as its baseline, and it lists the features that came from System V, from BSD and from GNU.[^1] Version 4.4.1 of GNU Make has a manual dated 26 February 2023.[^1]

## References
