Tutorial MUD, part 1.5: Makefile dependencies

While working on part 2, I noticed that I had forgotten one thing in part 1: Object file dependencies. Each object file compiled by the Makefile depends on its source file of course, but most of the time of one or more header files as well. So when a header file is changed, all object files that depend on that header file will be built and the whole application linked again.

As I want to keep the Makefile simple, I will not go into automatically generating these dependencies, instead we have to keep them up to date manually. If and when it begins to be too much trouble I will add a part where I add that to the Makefile.

So how do we add these dependencies then? It’s very simple, just add a new target with object file (e.g. main.o) as the target, and the source file it is built from and all header files the source file includes as dependencies.

For main.o it will look like this:

[codesyntax lang=”make”]

main.o: main.cpp tm.h


Every time we add a new source file we need to add a line like this. And every time a source file adds, removes or changes its list of (non-system) header files they need to be added as dependencies. With non-system header files, I mean the files included with double quotes (e.g. #include "tm.h") not the ones included with angle brackets (e.g. #include <string>.)

The Makefile has been updated to this with the tag part-1.5 in the github repository.

Leave a comment