next up previous index
Next: Exercise Up: Caring, Really Caring About Previous: Caring, Really Caring About

   
Automatic Compilation with Make

Explicit Makefile for program chi: 




chi: chi.o euler.o
        f90 -n32 -o chi chi.o euler.o

chi.o: chi.f90 EULER.kmo
        f90 -n32 -c chi.f90

euler.o: euler.f90
        f90 -n32 -c euler.f90

EULER.kmo: euler.f90
        f90 -n32 -c euler.f90

clean:
        rm -f *.o *.kmo core

clobber: clean
        rm -f chi





General syntax of a Makefile rule:
target: dependencies
<tab>   command_1
<tab>   command_2
...




Makefile for program chi using automatic variables: 

F90         = f90
F90_OPTS    = -n32

chi: chi.o euler.o
        $(F90) $(F90_OPTS) -o $@ $^

chi.o: chi.f90 EULER.kmo
        $(F90) $(F90_OPTS) -c $<

euler.o: euler.f90
        $(F90) $(F90_OPTS) -c $<

EULER.kmo: euler.f90
        $(F90) $(F90_OPTS) -c $<

clean:
        rm -f *.o *.kmo core

clobber: clean
        rm -f chi

The meaning of some automatic and defined variables:

Automatic variables are a mixed blessing: they can be hard to read.

Portable Makefiles: 

OS             = $(shell uname -s)
F90            = f90

ifeq ($(OS), IRIX)
   F90_OPTS    = -n32 -g
else
   F90_OPTS    = -g
endif

ifeq ($(OS), IRIX)
   MODULE_IFC  = EULER.kmo
else
   ifeq ($(OS), SunOS)
      MODULE_IFC  = euler.M
   else
      MODULE_IFC  =
   endif
endif

chi: chi.o euler.o
        $(F90) $(F90_OPTS) -o $@ $^

chi.o: chi.f90 $(MODULE_IFC)
        $(F90) $(F90_OPTS) -c $<

euler.o: euler.f90
        $(F90) $(F90_OPTS) -c $<

$(MODULE_IFC): euler.f90
        $(F90) $(F90_OPTS) -c $<

clean:
ifeq ($(OS), IRIX)
        rm -f *.o *.kmo core
else
   ifeq ($(OS), SunOS)
        rm -f *.o *.M core
   else
        rm -f *.o core
   endif
endif

clobber: clean
        rm -f chi

A Makefile for the HDF programs:

HDF_LIB        = /afs/ovpit.indiana.edu/@sys/HDF/lib
HDF_MODULES    = /afs/ovpit.indiana.edu/@sys/HDF/modules
OS             = $(shell uname -s)
F90            = f90
RM             = /bin/rm

ifeq ($(OS), IRIX)
   F90_OPTS    = -n32 -g
   LIBS        = -lmfhdf -ldf -ljpeg -lz -lm
   F90_MODULES = -I$(HDF_MODULES)
else
   ifeq ($(OS), SunOS)
      F90_OPTS    = -g
      LIBS        = -lmfhdf -lnsl -ldf -ljpeg -lz -lm
      F90_MODULES = -M$(HDF_MODULES)
   else
      F90_OPTS    = -g
      LIBS        = -lmfhdf -ldf -ljpeg -lz -lm
      F90_MODULES = -I$(HDF_MODULES)
   endif
endif

hdf-1: hdf-1.o
        $(F90) $(F90_OPTS) -o $@ $< -L$(HDF_LIB) $(LIBS)

hdf-1.o: hdf-1.f90
        $(F90) $(F90_OPTS) -c $(F90_MODULES) $<

hdf-2: hdf-2.o
        $(F90) $(F90_OPTS) -o $@ $< -L$(HDF_LIB) $(LIBS)

hdf-2.o: hdf-2.f90
        $(F90) $(F90_OPTS) -c $(F90_MODULES) $<

...

hdf-8: hdf-8.o
        $(F90) $(F90_OPTS) -o $@ $< -L$(HDF_LIB) $(LIBS)

hdf-8.o: hdf-8.f90
        $(F90) $(F90_OPTS) -c $(F90_MODULES) $<

clean:
        $(RM) -f *.o core

clobber: clean
        $(RM) -f hdf-1 hdf-2 hdf-3 hdf-4 hdf-5 hdf-6 hdf-7 hdf-8

Defining make rules for repetitive patterns: 

HDF_LIB        = /afs/ovpit.indiana.edu/@sys/HDF/lib
HDF_MODULES    = /afs/ovpit.indiana.edu/@sys/HDF/modules
OS             = $(shell uname -s)
F90            = f90
RM             = /bin/rm
HDF_EXAMPLES   = hdf-1 hdf-2 hdf-3 hdf-4 hdf-5 hdf-6 hdf-7 hdf-8

ifeq ($(OS), IRIX)
   F90_OPTS    = -n32 -g
   LIBS        = -lmfhdf -ldf -ljpeg -lz -lm
   F90_MODULES = -I$(HDF_MODULES)
else
   ifeq ($(OS), SunOS)
      F90_OPTS    = -g
      LIBS        = -lmfhdf -lnsl -ldf -ljpeg -lz -lm
      F90_MODULES = -M$(HDF_MODULES)
   else
      F90_OPTS    = -g
      LIBS        = -lmfhdf -ldf -ljpeg -lz -lm
      F90_MODULES = -I$(HDF_MODULES)
   endif
endif

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

%: %.o
        $(F90) $(F90_OPTS) -o $@ $< -L$(HDF_LIB) $(LIBS)

all: $(HDF_EXAMPLES)

clean:
        $(RM) -f *.o core

clobber: clean
        $(RM) -f $(HDF_EXAMPLES)



 
next up previous index
Next: Exercise Up: Caring, Really Caring About Previous: Caring, Really Caring About
Zdzislaw Meglicki
2001-02-26