I am new to Fortran. I am working on a research project where I am using an open source project that has several files distributed in multiple folders. i found the dependenc
You can do a Makefile which looks like that
F90=gfortran
FFLAGS = -O0
VPATH = modules:interfaces:subroutines:
MODOBJ = module1.o module2.o ...
your_executable: $(MODOBJ) main.o
$(F90) main.o -o your_executable
%.o:%.f90
$(F90) $(FFLAGS) -c $^ -o $@
VPATH
is the paths of the directories where your Makefile will look for source files, so if you compile your source in the root directory of modules/
, interfaces/
and subroutines/
, you just have to set up VPATH
like that.
If you have many objects and you don't want to write everything by hand, you can retrieve them using the following trick
F90 = gfortran
FFLAGS = -O0
VPATH = modules:interfaces:subroutines
SRCOBJ = $(wildcard modules/*f90)
MODOBJ = $(SRCOBJ:.f90=.o)
your_executable: $(MODOBJ) main.o
$(F90) main.o -o your_executable
%.o:%.f90
$(F90) $(FFLAGS) -c $^ -o $@
The wildcard
command in a Makefile allows you to use a joker *
; then you just have to say that in the strings you will retrieve in $(SRCOBJ)
, you want to substitute .f90
by .o
to get the filenames of your modules.
You can create your Makefiles as usual. The biggest problem should be the .mod
files. The easiest solution to this problem is to create a separate folder, where these files are stored and searched for.
This can be achieved with the -J
and the -module
flags for gfortran
and ifort
, respectively.