Preparing a Description File

Program make expects the required tasks be described in a file with name makefile or Makefile. This file is referred to as the description file.

In discussing make, we shall call program the target of the operation. A program is built from one or more files, called the prerequisites or dependents. Each of these files may in turn have other files as prerequisites. For example, your C program executable myprog might be generated by compiling two C source files, a.c and b.c. Hence, myprog has prerequisites a.c and b.c.

These prerequisites should be carefully stated in the description file. There are two important types of lines in a description file. A dependency line or rule line contains a colon, and a command line always starts with a tab.

To the left of the colon on the dependency line is a target; to the right of the colon are the target's prerequisites. Let us continue with the previous example. Since myprog has prerequisites a.c and b.c, we have

myprog :  a.c  b.c

The command lines just tells make how to make the target. For example, to generate myprog from a.c and b.c, what we might want to do is compiling both files and naming the output executable myprog with the following command:

$ gcc -o myprog a.c b.c
In this example, the GNU C compiler gcc is used to compile files a.c and b.c and place the executable to file myprog. The switch -o indicates the output file name. Note that $ is the UNIX prompt. Therefore, in the description file, we might have the following dependency line and command line:
myprog : a.c b.c
     gcc -o myprog a.c b.c
Please do not forget that the first character of a command line must be a tab.

Once the above two lines are placed into makefile or Makefile. From the UNIX prompt, type the following command, make will generate the executable \verb|myprog|:

make

However, before make really makes the executable for the user, it will check to see if the specified task is necessary. In fact, make will look at the creation/modification date of each prerequisite and the target. If one of the prerequisites has a creation/modification date that is newer than the creation/modification date of the target, the target is not up-to-date and regeneration is necessary. Then, make will generate a sequence of commands from the command line for execution by the UNIX shell. On the other hand, if the creation/modification date of the target is newer than the creation/modification dates of the prerequisites, the target is up-to-date and make will indicate this fact to the user and skip the specified task.