October 18, 2001, 4:32 PM — Make allows you to create, recreate, or update a file based on the existence and/or modification of one or more other files. The simplest way to understand make is to look at a programming example.
In C programming, a C source code file is created with vi or a similar editor. This file usually has the extension .c, so we'll start with a
program called hello.c. The source code is compiled into an object that usually has an .o extension, so in this case the object would be hello.o. The object
file is finally linked into a running program with no extension, which is called hello.
In terms of the make utility, the target hello.o depends upon hello.c, and the target hello depends upon hello.o.
A make file is a script-like file that describes this target/dependency relationship and also provides the commands needed to create or recreate one
target file from one or more other dependent files. The target, the dependency, and the command constitute a make rule. Sample make rules for this simple example are shown in the following listing.
hello.o : hello.c cc -c hello.c hello : hello.o cc hello.o -o hello
In the first rule, hello.o appears on a line followed by a colon and then hello.c. Note that the spaces around the colon are required. This is the make syntax used to indicate that hello.o depends on hello.c. Underneath that line is the command cc -c hello.c, which is the command to be executed if hello.o
needs to be created. The second rule is similar, stating that hello depends on hello.o and if hello is to be created, then the command cc hello.o -o hello will be used to create it.
The make utility uses a default make file that is named, appropriately enough, makefile or Makefile. If you place the lines shown above into a file named makefile, and then into a directory containing the source code hello.c, you can type the command:
make hello
and the hello program will be built. Typing the command a second time will cause a message indicating that the hello program is up to date, as in the following listing:
make hello 'hello' is up to date
What does "up to date" mean? It means that the modification date and timestamp on hello is greater than or equal to the modification date and timestamp
on all the files that hello depends on -- in this case hello.o and ultimately hello.c.
Whenever you run make, it creates an internal table of dependencies and then verifies the creation or modification date and timestamps and works out what
has to be created or recreated. This includes checking to see if a file doesn't exist at all. For example, the first time you run make hello, hello.o does not exist at all and is therefore considered out of date.
If the programmer modifies hello.c, its new modification date and timestamp force it to become out of date with respect to hello.o. If make hello were run
again after the program changes were created, hello.o would be rebuilt from hello.c. Then hello would be out of date with respect to hello.o so it would be rebuilt.
In this way make does two jobs. It checks whether a program or file needs to be rebuilt based on changes made to the original source code or dependency
file, and it simplifies the commands that have to be executed to recreate the program or target file. The make hello command is much easier to remember
and faster to type than cc -c hello.c followed by cc hello.o -o hello.
Beyond programming
Now we'll look at a more complicated process and take it out of the programming arena.
Assume that you've created a book containing five chapters named chap1.txt through chap5.txt. The process of assembling the chapters into a book involves:
- Combining the chapters
- Building a table of contents to place at the beginning of the book
- Paginating the resulting table of contents and chapters
- Building an index
- Appending the index to the end of the book
- Paginating one or more times to get the index pages numbered
Let's also assume that you have four programs available. The firsts, cat, concatenates text files, toc builds a table of contents, idx builds an index, and paginate renumbers the pages.
The first version of this process uses several intermediate files. The rules for this process are shown in the following listing. There is a problem with
this makefile that I will cover in a moment, but as it stands, it illustrates several new things about make that you need to know.













