MAKEFILES
Examples of the use of Makefiles


MAKEFILES is a directory of FORTRAN77 programs which illustrates how a "makefile" can be used to manage the compilation and loading of a FORTRAN77 program.

Usage:

A makefile is usually stored in a file named makefile.

The purpose of a makefile is to record or infer the commands necessary to compile and load a program (or, more generally, to "build" a program or object that depends on objects), to intelligently automate this task, and to efficiently update all objects that depend on an object that has itself just been updated.

The most natural example for which a makefile is useful would involve the relationship between several text files containing FORTRAN77 routines (with extension ".f"), the object files created by compiling separately each FORTRAN77 file (with extension ".o"), and the executable program that can be created by loading them all together (which, by default, is called "a.out", but which we will rename to "f77_simple".

We suppose we start with files f77_simple.f, midpoint.f, and f.f. If we wished to build the executable f77_simple, we need to create f77_simple.o, midpoint.o, f.o, and then load them together, and rename the result to f77_simple.

The "dependencies" or relationships between these files can be thought of as follows:

        f77_simple needs f77_simple.o, midpoint.o and f.o.
          The commands to create f77_simple are
          f77 f77_simple.o midpoint.o sub2.o
          mv a.out f77_simple
     
        f77_simple.o needs f77_simple.f.
          The command to create f77_simple.o is
          f77 -c f77_simple.f

        midpoint.o needs midpoint.f.
          The command to create midpoint.o is
          f77 -c midpoint.f

        f.o needs f.f.
          The command to create f.o is
          f77 -c f.f
      

The corresponding makefile records these relationships. Each dependency line lists a "target" (something you want to make), followed by a colon, and then a list of the components on which that target depends. There follow one or more command lines that tell how to put the components together to make the target. Note that each command line must begin with a TAB character. We will use the symbol --TAB--> to suggest this. Here is what the makefile would look like

        f77_simple : f77_simple.o midpoint.o f.o
        --TAB--> f77 f77_simple.o midpoint.o f.o
        --TAB--> mv a.out f77_simple
        f77_simple.o : f77_simple.f
        --TAB--> f77 -c f77_simple.f
        midpoint.o : midpoint.f
        --TAB--> f77 -c midpoint.f
        f.o : f.f
        --TAB--> f77 -c f.f
      

To create the program, type make f77_simple. If you just edited midpoint.f and want only to recompile it, type make midpoint.o. But if you just edited midpoint.f, and you want to recompile it, and then also recreate the program, then type make f77_simple. The make program will notice that midpoint.f has been updated, and automatically recompile it, and then rebuild f77_simple.

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

MAKEFILES is available in a C version, a C++ version and a FORTRAN77 version and a FORTRAN90 version.

Reference:

  1. Robert Mecklenburg,
    Managing Projects with GNU Make,
    O'Reilly, 2004,
    ISBN: 0596006101.
  2. Steve Talbot,
    Managing Projects with Make,
    O'Reilly, 1990,
    ISBN: 0-937-175-18-8.

Examples and Tests:

You can go up one level to the FORTRAN77 page.


Last revised on 04 December 2006.