How to create a makefile for a Fortran program using modules -
the challenge create makefile takes list of modules , not require me sort out precendence. example, modules are
mod allocations.f08 mod precision definitions.f08 mod unit values.f08 mod blocks.f08 mod shared.f08 mod parameters.f08 mod timers.f08
the main program characterize.f08
. error message is
fatal error: can't open module file ‘mprecisiondefinitions.mod’ reading @ (1): no such file or directory
the first statement in main program use mprecisiondefinitions
, module defined in mod precision definitions.f08
.
the following makefile, based upon creating fortran makefile, is:
# compiler fc := /usr/local/bin/gfortran # compile flags fcflags = -g -c -wall -wextra -wconversion -og -pedantic -fcheck=bounds -fmax-errors=5 # link flags flflags = # source files , objects srcs = $(patsubst %.f08, %.o, $(wildcard *.f08)) # program name program = a.out all: $(program) $(program): $(srcs) $(fc) $(flflags) -o $@ $^ %.mod: %.f08 $(fc) $(fcflags) -o $@ $< %.o: %.f08 $(fc) $(fcflags) -o $@ $< clean: rm -f *.o *.mod
for starters, recommend replace spaces in file names underscores or similar.
spaces universally used separators, , program started like
gfortran -c -o mod precision definitions.o mod precision definitions.f08
would interpret line 'create object file called mod
source files precision
, definitions.o
, mod
, precision
, , definitions.f08
. , while there ways it, increasing automation, have jump more , more hoops.
in contrast, works well:
gfortran -c -o mod_precision_definitions.o mod_precision_definitions.f08
i use command change spaces underscores:
for f in *.f08; mv "$f" ${f// /_}; done
next, wouldn't worry .mod
files. generated object files when compile module. while technically routine uses module requires .mod
file module, might claim in makefile
depends on object file itself.
so said, here's makefile use (with suspected inner-module dependencies added):
# find source files, create list of corresponding object files srcs=$(wildcard *.f08) objs=$(patsubst %.f08,%.o,$(srcs)) # ditto mods (they in both lists) mods=$(wildcard mod*.f08) mod_objs=$(patsubst %.f08,%.o,$(mods)) # compiler/linker settings fc = gfortran flflags = -g fcflags = -g -c -wall -wextra -wconversion -og -pedantic -fcheck=bounds -fmax-errors=5 program = characterize prg_obj = $(program).o # clean suffixes .suffixes: # set suffixes interested in .suffixes: .f08 .o # make without parameters make first target found. default : $(program) # compiler steps objects $(objs) : %.o : %.f08 $(fc) $(fcflags) -o $@ $< # linker $(program) : $(objs) $(fc) $(flflags) -o $@ $^ debug: @echo "srcs = $(srcs)" @echo "objs = $(objs)" @echo "mods = $(mods)" @echo "mod_objs = $(mod_objs)" @echo "program = $(program)" @echo "prg_obj = $(prg_obj)" clean: rm -rf $(objs) $(program) $(patsubst %.o,%.mod,$(mod_objs)) .phony: debug default clean # dependencies # main program depends on modules $(prg_obj) : $(mod_objs) # blocks , allocations depends on shared mod_blocks.o mod_allocations.o : mod_shared.o
Comments
Post a Comment