I currently compile programs based on modules (such as main program foo
which depends on module bar
) as follows:
gfortran -c bar.f90
gfortran -o foo.exe foo.f90 bar.o
This works fine when foo.f90
and bar.f90
are in the same directory. How do I specify a directory where gfortran should look for bar.o
when I call use bar
in foo.f90
? (i.e. I don't want to specify that the compiler should link bar.o
specifically, I just want it to go find it.)
You can tell gfortran where your module files (.mod files) are located with the -I
compiler flag. In addition, you can tell the compiler where to put compiled modules with the -J
compiler flag. See the section "Options for directory search" in the gfortran man page.
I use these to place both my object (.o files) and my module files in the same directory, but in a different directory to all my source files, so I don't clutter up my source directory. For example,
SRC = /path/to/project/src
OBJ = /path/to/project/obj
BIN = /path/to/project/bin
gfortran -J$(OBJ) -c $(SRC)/bar.f90 -o $(OBJ)/bar.o
gfortran -I$(OBJ) -c $(SRC)/foo.f90 -o $(OBJ)/foo.o
gfortran -o $(BIN)/foo.exe $(OBJ)/foo.o $(OBJ)/bar.o
While the above looks like a lot of effort to type out on the command line, I generally use this idea in my makefiles.
Just for reference, the equivalent Intel fortran compiler flags are -I
and -module
. Essentially ifort replaces the -J
option with -module
. Note that there is a space after module, but not after J.