Specify directory where gfortran should look for modules

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.

Leave a Comment