Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a makefile for Fortran with modules?

gg=mpif90
DEPS=matrix.mod
OBJ=  main.o sub1.o

main.out: $(OBJ)
    $(gg) -o $@ $^
%.mod:%.90 %.o
    $(gg) -c -o $@ $^

%.o:%.f90 $(DEPS)
    $(gg) -c -o $@ $^

.PHONY: clean

 clean:
    -rm -f *.o *~

Look. The main program is main.f90.sub1.f90 will be called by main.f90. Both will use matrix.f90 which is a module. I know I can directly generate the executable program without compile then link. But I do not like that way.

like image 806
Taitai Avatar asked Oct 31 '22 04:10

Taitai


1 Answers

The mod file is only a by-product of compiling %.o, you shouldn't use -o $@ here, change it to

%.mod: %.90
    $(gg) -c  $^

This will work for most cases, but not all. That's because the name of mod file depends only on the module name, it has nothing to do with the source file name. So the safest way is to specify the dependency explictly.

matrix.mod: matrix.f90
    $(gg) -c matrix.f90

Sometimes one f90 source file can contain two or more modules.

matrix33.mod matrix99.mod: matrix.f90
    $(gg) -c matrix.f90
like image 100
gdlmx Avatar answered Nov 09 '22 12:11

gdlmx