What is a make file?



Introduction for UNIX make

make is a UNIX utility that is used to manage source code compilation. By default make takes as input a make file (a file called Makefile or makefile). A make file specifies a set of dependency rules and of actions that should be performed for these rules. The general syntax of a rule/action pair is:

myprogram: myprogram.c
________gcc myprogram.c -o myprogram
where "________" denotes a TAB character, "myprogram" is the rule's target, "myprogram.c" is the rule dependencies, and "gcc .." is the command. WARNING: the TAB character is necessary, and cannot be replaced by white spaces!

The rule above says: "To generate myprogram, check whether myprogram.c was modified after the current version of myprogram, if it exists, was created. If myprogram.c was modified or if myprogram does not exist, then execute the command "gcc myprogram.c -o myprogram" (which generates myprogram). This is accomplished by typing "make myprogram" at the UNIX prompt.

The advantages of using make are that (i) there is no need to type long compilation command lines; (ii) make only compiles what is needed given what source files have been changed.

It is also customary to declare a rule with a target called "clean" that removes all executable and object files, as:

clean:
________/bin/rm -f myprogram
Note that target "clean" has no dependencies, meaning that typing "make clean" at the UNIX prompt will always execute the command.

Note that by default, typing "make" at the UNIX prompt without specifying a target results in the execution of the first rule in the make file. It is typical to have the first rule be something like:

default: myprogram
which ensures that just typing "make" will have the desired behavior.


What you'll need for the class

For the class you'll only need to write very simple make files, and here is a small example. You should base all your make files on this one for assignments in C. For assignments in Java make files are a bit overkill as it is often possible to compile the whole source by typing the command "javac *.java". Nevertheless, we require that you use a simple make file such as the one there. Remember that the make file must be saved under the name "Makefile" or "makefile". For the other two languages used in the class, LISP and Prolog, there is no need for a make file as programs are not compiled but interpreted.


More information

Here is a great tutorial on make.