Lesson 1f83acaMake Graph Foundation1/10

Give a bare Make invocation a stable entry point

Add a conventional Makefile, let all become the default goal, and declare it phony so the same bare command can grow with the build.

01

Tell make what to do

A makefile tells make what work it knows how to perform. This project uses Makefile, the conventional filename recommended by GNU Make. Because GNU Make recognizes that name by default, a bare make finds it automatically.

Compare missing and empty makefiles

In an empty directory, run make before and after creating an empty Makefile.

step 1/3
interactive replay

Select the terminal to begin, or choose Auto-fill.

02

Let all become the default goal

A rule names one or more targets before the colon, lists their prerequisites after it, and may place a recipe below. A goal is a target that Make strives ultimately to update. make all names all explicitly; a bare make asks Make to choose the default goal.

Unless .DEFAULT_GOAL sets it explicitly, GNU Make uses the first eligible target it encounters. A dot-prefixed target without a slash and a pattern-rule target do not count. That is why .PHONY on line 14 does not become the default: all on line 15 is the first eligible target.

all is the conventional target for making every top-level target the makefile knows about. Here it has no prerequisites and no recipe, so it is only the scaffold for an aggregate target. Prerequisites can be attached later while the same bare make interface stays in place.

Makefile:1-15

8 focused lines

View in repository
Loading source...

Run all explicitly and by default

Name the goal first, then omit it to confirm a bare make selects the same goal.

step 1/2
interactive replay

Select the terminal to begin, or choose Auto-fill.

03

Keep all from being treated as a file

Most targets name files, but all names build behavior. Without .PHONY, a file named all would make Make treat this target as a real file. With the current empty rule, Make would call it up to date; if a recipe were added later, timestamps could cause Make to skip it.

Writing .PHONY: all makes all a prerequisite of the special .PHONY target and therefore a phony target itself. Make considers a phony target out of date whenever it is reached, regardless of whether a same-named file exists, so a future recipe would run in every invocation that reaches it. There are no prerequisites or recipe yet; the declaration establishes the intended non-file semantics in advance.

Makefile:8-15

8 focused lines

View in repository
Loading source...

Keep a file from satisfying all

Create a file named all; the phony declaration keeps the default goal independent of it.

step 1/3
interactive replay

Select the terminal to begin, or choose Auto-fill.

Finished this lesson?

Completion is saved on this device and used to resume the course.