out of phase
out of phase

Reputation: 13

Fortran makefile recompiling every time

I'm trying to write a Fortran makefile: there is a single "main" file, but I've placed my modules in separate .f90 files. The makefile attached works, however even if I make no changes it forces a recompile. I'm running x86-64 Linux with gfortan 4.7.2 and GNU Make 3.81.

FC = gfortran

FCFLAGS += -ffree-line-length-none -finit-local-zero 

#FCFLAGS += -fbounds-check -ffpe-trap=invalid

all: new

new: ALE.o curvilinear.o new.o
    $(FC) $(FCFLAGS) -o new new.o ALE.o curvilinear.o

new.o: ALE.mod curvilinear.mod new.f90
    $(FC) $(FCFLAGS) -c new.f90

ALE.o: ALE.f90
    $(FC) $(FCFLAGS) -c ALE.f90

ALE.mod: ALE.o ALE.f90

curvilinear.o: ALE.o curvilinear.f90
    $(FC) $(FCFLAGS) -c curvilinear.f90

curvilinear.mod: curvilinear.o curvilinear.f90

clean:
    rm ale.mod curvilinear.mod ALE.o curvilinear.o new new.o 

Output:

$ make clean
rm ale.mod curvilinear.mod ALE.o curvilinear.o new new.o
$ make
gfortran -ffree-line-length-none -finit-local-zero  -c ALE.f90
gfortran -ffree-line-length-none -finit-local-zero  -c curvilinear.f90
gfortran -ffree-line-length-none -finit-local-zero  -c new.f90
gfortran -ffree-line-length-none -finit-local-zero  -o new new.o ALE.o curvilinear.o
$ make
gfortran -ffree-line-length-none -finit-local-zero  -c new.f90
gfortran -ffree-line-length-none -finit-local-zero  -o new new.o ALE.o curvilinear.o

Why is "new" being recompiled when no changes have been made?

Upvotes: 1

Views: 894

Answers (2)

sigma
sigma

Reputation: 2963

Didier's answer probably already solves your question, but I would like to add a nice and clean suggestion for Fortran makefiles (example for one main program):

# Main program
all: program
program: mod1.o mod2.o mod3.o

# Fortran module dependencies
mod1.o: mod2.o mod3.o

# Binary/object rules
%: %.o
        $(FC) $(FCFLAGS) -o $@ $^ $(LDFLAGS)

%.o: %.f90
        $(FC) $(FCFLAGS) -c $<

Upvotes: 2

Didier Trosset
Didier Trosset

Reputation: 37467

As per your Makefile, new.o depends on ALE.mod and curvilinear.mod.

I guess these file do never exist, as they have dependencies, but no rule to make them.

Indeed, When trying to build new, make does not found the *.mod files. Hence, make runs the rules to make them, but there's no rule. However, make think that it has build them, and continues the build operation, thinking that these *.mod dependencies have been just rebuild, and are thus recent, triggering the rebuild of new.

To fix this, you should replace the line

new.o: ALE.mod curvilinear.mod new.f90

by

new.o: new.f90

Upvotes: 1

Related Questions