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:
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.