Deciding What to Rebuild

Makefile rules describe relationships between files and the commands that update them. When you run make, these rules let it reuse current outputs and rebuild affected ones. You need to distinguish what a rule promises, which inputs it names, and when its commands should run. This lesson uses explicit file rules in GNU make, with ordinary prerequisites and successful recipes that update their named outputs.

Anatomy of an explicit rule

A target is the output file named before the colon. A prerequisite is an input named after the colon that the target depends on. The commands below the rule form its recipe.


makefile
report.o: report.c report.h
	cc -c report.c -o report.o


Here, report.o is the target; report.c and report.h are prerequisites. The command tells make how to produce the output. By default, a recipe line begins with a literal tab. Spaces that look like a tab do not satisfy this syntax.


The prerequisite list and the recipe serve different purposes. The list tells make when an output may need updating. The recipe tells it what action to perform. Naming a header in the command is not a substitute for listing it as a prerequisite.



The timestamp decision

A file's modification timestamp supplies the comparison. For an ordinary file target with a recipe, make first considers its prerequisites and updates any that need rebuilding. It then applies the governing decision:


Rebuild Tif T is missing, or some ordinary prerequisite P has t(P)>t(T).\text{Rebuild }T\quad\text{if }T\text{ is missing, or some ordinary prerequisite }P\text{ has }t(P)>t(T).


Here, t(P)t(P) and t(T)t(T) mean the modification timestamps of the prerequisite and target. A prerequisite must be strictly newer; an equal timestamp does not satisfy the newer-than test.


Situation after prerequisites are considered

Decision for the target

Target is missing

Run its recipe

Target exists and a prerequisite is newer

Run its recipe

Target exists and all prerequisites are older or equal

Skip its recipe


This comparison uses timestamps rather than a comparison of file contents. Editing an input normally gives it a newer timestamp. Changing contents while preserving an old timestamp can therefore defeat the expected decision.


Example: An existing output recorded at 14:12 remains current when its inputs are recorded at 14:10 and 14:12. If one input is instead recorded at 14:15, the output needs rebuilding.

Dependencies determine the affected chain

Rules connect into a dependency graph. An intermediate file can be a target in one rule and a prerequisite in another.


Source files lead to their object files, which both lead to an executable.


If helper.c becomes newer than helper.o, make updates helper.o before deciding whether to update app. The refreshed object can then make app stale. An unchanged main.o can be reused. Independent prerequisites need not have a unique execution order; their relationship requires only that necessary prerequisite updates finish before the dependent recipe starts.


Make can follow only the dependencies it knows about. If both source files use a shared header, both object rules should list that header. Omitting one relationship may leave an old object in place even though the program needs a rebuild.



Some targets name actions rather than output files. GNU make supports phony targets for this purpose:


makefile
.PHONY: clean
clean:
	rm -f app main.o helper.o


The declaration makes make clean consider the action regardless of whether a file named clean exists. Keep such maintenance actions out of ordinary file prerequisite lists unless you deliberately want their repeated effect.

What to remember

  • Read the target, prerequisites, and recipe as three distinct parts.
  • Update prerequisites before checking the dependent target.
  • Rebuild a missing target or one with a strictly newer prerequisite.
  • Declare every input relationship needed for a reliable rebuild.
  • Declare action targets phony when their names do not represent output files.

Example: Rebuilding a monitoring utility

A monitoring utility uses the explicit Makefile Build Rules below. An incremental build reuses outputs that remain current and updates the affected ones. Each recipe succeeds and gives its target a timestamp later than all timestamps initially listed. No files change during the build.


makefile
monitor: sensor.o sample.o
	cc sensor.o sample.o -o monitor
sensor.o: sensor.c calibration.h
	cc -c sensor.c -o sensor.o
sample.o: sample.c
	cc -c sample.c -o sample.o


File

Modification time on the same day

sensor.c

10:01

sensor.o

10:02

sample.c

10:03

sample.o

10:04

monitor

10:05

calibration.h

10:06


The sensor.o recipe takes 4 s, the sample.o recipe takes 7 s, and the monitor recipe takes 2 s. Recipes execute serially, and make's own overhead is negligible.


Example: Determine the work required when the developer runs make monitor.

a) Which recipes execute, and what order is required?

First check the prerequisites of each object. The newest prerequisite of sensor.o is calibration.h, recorded at ________. This is later than the object's timestamp of ________, so the sensor.o recipe executes.


For sample.o, the only prerequisite is sample.c at 10:03. That input is older than the object at 10:04, so its recipe is skipped.


After the updated sensor.o receives a new timestamp, it is newer than the existing monitor. The executable recipe therefore runs after the object recipe finishes.


Answer: Execute the sensor.o recipe, then the monitor recipe. Reuse sample.o without executing its recipe. The executable's initially newer timestamp does not protect it from an object updated during this invocation.

b) How long does this incremental build take?

Only the selected recipes contribute time. Updating the affected object takes ________, and updating the executable takes ________. Since they execute serially, add their durations:


Tincremental=4 s+2 s=6 s.T_{\text{incremental}}=4\text{ s}+2\text{ s}=6\text{ s}.


Answer: The incremental build takes 6 s. Including the unchanged object's 7 s recipe would count work that make skips.

c) How long would a full rebuild take, and how much time does the incremental build save?

For this comparison, assume all three output files are removed before a full rebuild. Both object recipes must then execute, followed by the executable recipe. Their combined object-update time is ________, obtained by adding 4 s and 7 s.


Tfull=(4+7+2) s=13 s.T_{\text{full}}=(4+7+2)\text{ s}=13\text{ s}.


Compare the two builds under the same serial execution assumption:


Tsaved=Tfull−Tincremental=13 s−6 s=7 s.T_{\text{saved}}=T_{\text{full}}-T_{\text{incremental}}=13\text{ s}-6\text{ s}=7\text{ s}.


Answer: A full rebuild takes 13 s, so the incremental build saves 7 s. The saving matches the duration of the one skipped object recipe, which provides a useful check on the dependency analysis.

A developer writes the following explicit Makefile rule, with a literal tab before the command. Which description correctly identifies its parts?


makefile
index.o: index.c index.h
	cc -c index.c -o index.o
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A GNU Makefile contains a pattern rule with this header and a valid recipe:


makefile
build/%.o: src/%.c config.h


The build directory and both required input files exist. For the requested target build/session-log.o, the substring matched by % is , and substituting that substring into the patterned prerequisite produces . Enter the substring and full prerequisite path exactly.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A project has the rules below. All recipes succeed and update their target timestamps. The listed timestamps are on the same day, and no files change during the build. How many recipe command lines execute when the developer runs make meter? Answer in command lines, as an exact integer.


makefile
meter: read.o display.o
	cc read.o display.o -o meter
read.o: read.c units.h
	cc -c read.c -o read.o
display.o: display.c units.h
	cc -c display.c -o display.o


File

Modification time

read.c

08:01

display.c

08:02

read.o

08:05

display.o

08:06

meter

08:07

units.h

08:10

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice