[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Correct dependency for Fortran .MOD and .O files

From: Luke Shumaker
Subject: Re: Correct dependency for Fortran .MOD and .O files
Date: Thu, 27 Jan 2011 21:11:50 -0500

On Thu, 2011-01-27 at 17:28 +0100, Tobias Burnus wrote:
> Hello,
> I have a Makefile-dependency question regarding Fortran: In Fortran one 
> can have so-called modules. If one compiles such a file, a .mod file is 
> generated which contains the public interface and a .o file which 
> contains the binary.
> If one changes the source file (.f90) without modifying the interface, 
> the file should be compiled - and the relinking happening, but no other 
> files should be compiled.
> If the source file is changed such that the the interface (.mod file) 
> changes, all source files depending on the .mod file should be recompiled.
> The question is how one can squeeze this into a Makefile; what I can 
> tune is whether the .mod file gets "touched" (file modification date 
> changed) or not after modification.
> The following is a default dependency file, which in principle nicely 
> works, but I either get a complete recompilation as soon as the date of 
> the .mod file changes.
> myprogram: myprogram.o mymodule.o
>       $(FC) -o myprogram myprogram.o mymodule.o
> mymodule.o mymodule.mod: mymodule.f90
>       $(FC) -c mymodule.f90
> myprogram.o: myprogram.f90 mymodule.mod
>       $(FC) -c myprogram.f90
> Assuming the .mod date does not change, the Makefile above will cause a 
> complete recompilation of "mymodule" every time as "mymodule.mod" is 
> always older than "mymodule.f90". I could try the following, but it does 
> not work if one either has the wrong order in the "myprogram:" 
> dependency list or when one calls "make myprogram.o" (assuming that 
> mymodule.f90 has changed such that the interface is different - and that 
> myprogram.f90 needs this change).
> Any idea? Is is better for the compiler to change the .mod file 
> modification date or not?
> Background: gfortran currently does not, but it causes obviously some 
> problems; cf.
> Tobias

After some Googling, the standard option seems to be to have files
depend on `%.o'. This seems to be the best option; though it means files
get recompiled more than they have to, it is the only way that seems to
be in line with how makefiles work.  If you really wanted to use .mod as
the dependency though, add the line `touch filename.mod' to the end of
the recipe.

# default
all: myprogram
# pattern rules
%: %.o
        $(FC) $(FCFLAGS) -o $@ $^ $(LDFLAGS)
%.o %.mod: %.f90
        $(FC) $(FCFLAGS) -c $<
        # touch $*.mod
# dependencies
myprogram: mymodule.o
myprogram.o: mymodule.o

~ LukeShu

reply via email to

[Prev in Thread] Current Thread [Next in Thread]