Writing Makefiles

Pattern Rules

A pattern rule can be used to define a generic recipe for turning a file of type X into a file a type Y for example, compiling program.c into program.o. A pattern rule can be defined as follows

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

Match files of the form *.o and *.c respectively

$<

References all the dependencies of the target, in this case the *.c file.

$@

References the target itself, in this case the .o file

Examples

.POSIX:

CC = gcc
CFLAGS = -Wall $(shell pkg-config --cflags xcb-image)
LDLIBS = $(shell pkg-config --libs xcb-image)

default: main

debug: CFLAGS += -g
debug: main

main: main.o
   $(CC) $< -o $@ $(LDLIBS)

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