Spring, 2016
Jewett
The make program
ATMS 502 / CSE 566
Numerical fluid dynamics
Make

Make is a Unix/Linux utility that helps you compile your program efficiently and quickly -- important when your program includes many routines.  Advantages include compiling just those routines that you have recently changed; when doing a lot of work, this will speed up your code testing.  So, to make best use of make, put most subroutines in separate files, and let Make compile / link them for you.

Make uses a file you have created, typically named "makefile" or "Makefile", to know which files contain the routines you need to compile your program.  A sample Makefile is on Stampede at ~tg457444/502/Pgm4/Fortran/Makefile.  Provided you have copied the fortran files to your directory, you can use make to compile and link the test interpolation program by simply typing

    make

upon which it automatically determines which files need compiling, and does so.  The first time you do this, it compiles everything:

login4% pwd
/share/home/00478/tg457444/502/Pgm4/Fortran
login4% ls
contr.f90  dointerp.f90  Makefile  nestwind.f90  test_interp.f90

login4% make                          
(what is shown below is done automatically by make, for you:)
ncargf90 -c test_interp.f90    
(compiles test_interp)
ifort -O2 -c test_interp.f90 -L/share/home/00478/tg457444/ncarg/lib -L/usr/X11R6/lib64 -lncarg -lncarg_gks -lncarg_c -lXpm -lX11 -lXext
ncargf90 -c dointerp.f90        
(compiles dointerp)
ifort -O2 -c dointerp.f90 -L/share/home/00478/tg457444/ncarg/lib -L/usr/X11R6/lib64 -lncarg -lncarg_gks -lncarg_c -lXpm -lX11 -lXext
ncargf90 -c contr.f90              
(compiles contr)
ifort -O2 -c contr.f90 -L/share/home/00478/tg457444/ncarg/lib -L/usr/X11R6/lib64 -lncarg -lncarg_gks -lncarg_c -lXpm -lX11 -lXext
ncargf90 -O -o test_interp test_interp.o dointerp.o contr.o  
(this step takes the compiled files above and creates your running program)
ifort -O2 -O -o test_interp test_interp.o dointerp.o contr.o -L/share/home/00478/tg457444/ncarg/lib -L/usr/X11R6/lib64 -lncarg -lncarg_gks -lncarg_c -lXpm -lX11 -lXext

Suppose I now change only test_interp.f.  Upon typing make again, the make utility compiles only that file, and creates a new executable program test_interp:

login4% make
ncargf90 -c test_interp.f90
                                                                      (compile step, of only file needing compiling)
ifort -O2 -c test_interp.f90 -L/share/home/00478/tg457444/ncarg/lib -L/usr/X11R6/lib64 -lncarg -lncarg_gks -lncarg_c -lXpm -lX11 -lXext
ncargf90 -O -o test_interp test_interp.o dointerp.o contr.o
            (link step, to create program test_interp)
ifort -O2 -O -o test_interp test_interp.o dointerp.o contr.o -L/share/home/00478/tg457444/ncarg/lib -L/usr/X11R6/lib64 -lncarg -lncarg_gks -lncarg_c -lXpm -lX11 -lXext


An example

Hopefully the Program 4 test Makefile can be a useful template for you to use.  Ignoring the comments at the top, it contains:

F90    = ncargf90
OPTIONS    = -O
OBJECTS = test_interp.o dointerp.o contr.o

test_interp:    $(OBJECTS)
    $(F90) $(OPTIONS) -o test_interp $(OBJECTS)

%.o:    %.f90
    $(F90) -c $<


What is this doing?  In order:
  • F90 defines the fortran compiler.  You could name it anything (doesn't have to be "F90"); in make, this is a variable.
  • OPTIONS are optimization choices.  If you use, for example, "-O0" (last part of this is digit zero), compiliation will be faster due to the lower optimization, but your program will run more slowly.
  • OBJECTS are a list of the object files used in compiling your program.  There is one for every main program or subroutine which resides in a distinct file.  Object files are created, usually without telling you, when you compile your program the usual way.  Their listing here tells make which files/routines are needed to create your executable program.

  • %.o is a compact way of telling make that in order to compile (and produce an intermediate object file) a fortran (".f90") source file, it needs to follow the commands on the next line.  Here, that next line says to use fortran compiler F90, defined earlier.

  • .c.o similarly tells make that to compile a ".c" file and create an intermediate object file, it should use ncargcc to do so.
For more information try these links:
  1. http://www.math.tau.ac.il/~danha/courses/software1/make-intro.html
  2. http://mrbook.org/tutorials/make/
  3. http://en.wikipedia.org/wiki/Make_%28software%29
  4. Using Make for a simple Fortran project:  here
  5. Overview of Makefiles in Linux:  here
  6. Makefiles for beginners:  here