Examples of the use of Makefiles

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


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 F90 routines (with extension ".f90"), the object files created by compiling separately each F90 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 "f90_simple".

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

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

        f90_simple needs f90_simple.o, midpoint.o and f.o.
          The commands to create f90_simple are
          f90 f90_simple.o midpoint.o sub2.o
          mv a.out f90_simple

        f90_simple.o needs f90_simple.f90.
          The command to create f90_simple.o is
          f90 -c f90_simple.f90

        midpoint.o needs midpoint.f90.
          The command to create midpoint.o is
          f90 -c midpoint.f90

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

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

        f90_simple : f90_simple.o midpoint.o f.o
        --TAB--> f90 f90_simple.o midpoint.o f.o
        --TAB--> mv a.out f90_simple
        f90_simple.o : f90_simple.f90
        --TAB--> f90 -c f90_simple.f90
        midpoint.o : midpoint.f90
        --TAB--> f90 -c midpoint.f90
        f.o : f.f90
        --TAB--> f90 -c f.f90

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


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


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


  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 FORTRAN90 page.

Last revised on 04 December 2006.