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:
$(F90): whatever F90 has been defined to be
$(F90_OPTS): whatever F90_OPTS has been defined to be
$@: a target, e.g., chi in the first instruction
$^: all explicitly listed dependencies for that target,
e.g., chi.o and euler.o in the first instruction
$<: the first dependency, e.g., chi.f90
in the second instruction
Automatic variables are a mixed blessing: they can be hard to read.
*.kmo with *.M. Also, for SunOS we don't
need the -n32 flag.
OS = $(shell uname -s)
$(OS) is we can use Makefile
conditionals to make it more portable. This feature is
available in GNU make only.
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:
.f90 file into
a .o file looks the same for all .f90
files in this Makefile:
$(F90) $(F90_OPTS) -c $(F90_MODULES) $<
.f90 files as a
default:
%.o: %.f90
$(F90) $(F90_OPTS) -c $(F90_MODULES) $<
Makefile:
hdf-1.o: hdf-1.f90 hdf-2.o: hdf-2.f90 hdf-3.o: hdf-3.f90 hdf-4.o: hdf-4.f90 hdf-5.o: hdf-5.f90 hdf-6.o: hdf-6.f90 hdf-7.o: hdf-7.f90 hdf-8.o: hdf-8.f90
%: %.o
$(F90) $(F90_OPTS) -o $@ $< -L$(HDF_LIB) $(LIBS)
hdf-1: hdf-1.o hdf-2: hdf-2.o hdf-3: hdf-3.o hdf-4: hdf-4.o hdf-5: hdf-5.o hdf-6: hdf-6.o hdf-7: hdf-7.o hdf-8: hdf-8.o
Makefile
contains only the .f90 files (and none of,
say, hdf-1.c files), you can remove the
dependencies too, and just replace all of them with
the following:
HDF_EXAMPLES = hdf-1 hdf-2 hdf-3 hdf-4 hdf-5 hdf-6 hdf-7 hdf-8 ... all: $(HDF_EXAMPLES)
Makefile will work both on
the Ships Cluster and on the Burrow Sun Cluster
in Lindley Hall (LH004)
hdf-1 through
hdf-8 and will remove all intermediary
files, hdf-1.o through hdf-8.o. This is
not equivalent to doing make clean though.
GNU make does the last step out of its own initiative.
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)