>_ IzzyLab

Hands-on task

This task is step 6 of the course “Make: builds and automation”

Rebuild only when the source changes

medium~5 min

In /root/proj there is main.c. Write a Makefile with an app target that depends on main.c and is built by copying (cp main.c app). The essence is in the dependency: make should rebuild app only if main.c is newer than the built app, and do nothing if the source hasn't changed.

What you'll practise:
  • make
  • target dependencies
  • incremental build
  • mtime

Topic breakdown

The core idea of make is the incremental build: a target is rebuilt only if it's out of date relative to its dependencies. For this the target is declared with prerequisites (target: dependency), and make compares modification times (mtime): if the dependency is newer than the target or the target doesn't exist yet — the recipe runs, otherwise make reports "up to date". Without a declared dependency make has nothing to compare mtime against and can't track source changes.

What's checked

  1. make builds app from main.c
  2. app is not rebuilt without changes
  3. app is rebuilt after main.c changes

What it looks like