>_ IzzyLab

Hands-on task

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

Object list from wildcard and substitution

hard~10 min

In /root/proj there are several *.c files (how many and how they're named is not known in advance). Write a Makefile that finds all the .c files itself via $(wildcard *.c), derives a list of .o files from their list by suffix substitution ($(SRC:.c=.o)), and builds each .o from its .c with a pattern rule (which is really just a copy). A bare make (the all target) should build an .o for each .c. The Makefile is handy to write in the Editor tab above the terminal: in a Makefile it puts a real TAB at the start of a recipe line.

What you'll practise:
  • make
  • wildcard
  • suffix substitution
  • pattern rules

Topic breakdown

A typical Makefile avoids hardcoded names through three mechanisms. The $(wildcard ...) function expands into a list of files that actually exist matching a pattern; a suffix-substitution reference ($(VAR:.c=.o)) turns one list into another by changing extensions; and the pattern rule %.o: %.c describes how to produce any file of one kind from its counterpart. Together they yield a Makefile independent of specific source names: add a file and it's picked up automatically.

What's checked

  1. an .o is built for each .c
  2. wildcard and suffix substitution are used

What it looks like