Makefiles

Compiling programs can be quick and easy with makefiles. Make is indispensable for managing large programs, but is useful for smaller programs as well.

As an example, consider a program consisting of the files main.f, file1.f, and file2.f. Here is a sample makefile to take these three files and generate an executable called main.


OBJ = main.o file1.o file2.o .f.o: f77 -c $< main: $(OBJ) f77 -o main $(OBJ)

Important formatting note: The lines after .f.o: and main: begin with a TAB character.

Executing the makefile

This makefile is executed by the command make main.

How does it run?

After make main is typed, make ensures that all the files after main: exist. If any do not exist, make looks for a rule to create it. In this case, the lines after .f.o tell it how to make a .o file from a .f file. Once all the appropriate .o files exist, make runs f77 -o main $(OBJ) to create the executable.

Recompiling

Make also compares the modification time of the .f file with that of the .o file. If the .f file has been modified since it was last compiled, make will recompile the .f file.

In this example, suppose main.f had been modified. Make would recompile main.f, but not file1.f or file2.f .

What about include files?

Suppose that main.f and file1.f include a file param.h and that we would like these files recompiled if it changes. Add the line
main.o file1.o: param.h
to the end of the makefile.

Line by line description

Go to the example details page.

More Information

For more information about makefiles, type man make, or see the online Make manual


Back to Computer Information