Makefile with .mod files

Erik Schnetter schnetter@cct.lsu.edu
Sat Nov 3 19:23:00 GMT 2007


On Nov 3, 2007, at 14:11:03, Ignacio Fernández Galván wrote:

> --- FX Coudert <fxcoudert@gmail.com> wrote:
>
>> This is a change from 4.2, IIRC, and it was done because users
>> complained that .mod files had their timestamps updated while not
>> changing, which triggered massive useless recompilations because of
>> Makefile dependencies ;-)
>
> Fair enough, I think that's OK
>
>>> So, is there a solution for this?
>>
>> Yes, I believe there is; the following Makefile seems to work fine:
>>
>> all: a.o b.o
>>
>> a.o: a.f90 b.mod
>>          gfortran -c $<
>>
>> b.o: b.f90
>>          gfortran -c $<
>>
>> b.mod: b.f90 b.o
>>          @true
>>
>> a.mod: a.f90 a.o
>>          @true
>
> It works... until one deletes the .mod files (which can happen by
> accident), then make doesn't know how to build them. I guess I could
> use something like "test -e $@ || gfortran -c $<" instead of  
> "@true"...
> Anyway, there is another (minor) problem: with that solution make  
> still
> thinks there is something to build and does not inform that everything
> is up to date, even though it doesn't actually build anything.

Since .mod files and .o files are generated at the same time, I  
prefer the following:

all: a.o b.o

a.o: a.f90 b.o
	gfortran -c $<

b.o: f.90
	gfortran -c $<

Of course, in reality you use rules, so that would read:

all: a.o b.o

%.o: %.f90
	gfortran -c $*.f90

a.o: b.o

Finally, if you use GNU make you can also write:

a.o a.mod: a.f90
	gfortran -c $<

to indicate that both files are created.  (At least I think this is  
possible; maybe it is only possible with rules instead of explicit  
targets, or there is another gotcha which I'm missing at the moment.   
As I said above, I don't usually mention .mod files in Makefiles.)

-erik

-- 
Erik Schnetter <schnetter@cct.lsu.edu>

My email is as private as my paper mail.  I therefore support encrypting
and signing email messages.  Get my PGP key from www.keyserver.net.



-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20071103/b9998c00/attachment.sig>


More information about the Fortran mailing list