Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify directory where gfortran should look for modules

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.)

like image 899
astay13 Avatar asked Jan 13 '12 19:01

astay13


2 Answers

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.

like image 70
Chris Avatar answered Sep 26 '22 06:09

Chris


When compiling a Fortran source code that contains modules, a .mod (typically the name of the file is the same as the module name) file is created along with the object file. The .mod file should be in the same directory as the source file that is using that module, or it should be pointed to at compilation time using -I flag:

gfortran -c bar.f90 
gfortran -c foo.f90 -I$PATH_TO_MOD_FILE
gfortran -o foo.exe foo.o bar.o

Note that .mod needs to exist at the foo.f90 compilation time.

like image 44
milancurcic Avatar answered Sep 23 '22 06:09

milancurcic