指定 gfortran 应在其中查找模块的目录 [英] Specify directory where gfortran should look for modules
问题描述
我目前编译基于模块的程序(如主程序foo
依赖于模块bar
)如下:
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
当 foo.f90
和 bar.f90
在同一目录中时,这可以正常工作.当我在 foo.f90
中调用 use bar
时,如何指定 gfortran 应在其中查找 bar.o
的目录?(即我不想指定编译器应该专门链接 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.)
推荐答案
您可以使用 -I
编译器标志告诉 gfortran 模块文件(.mod 文件)的位置.此外,您可以使用 -J
编译器标志告诉编译器将已编译模块放在何处.请参阅 gfortran 手册页中的目录搜索选项"部分.
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.
我使用它们将我的对象(.o 文件)和我的模块文件放在同一个目录中,但与我的所有源文件放在不同的目录中,所以我不会弄乱我的源目录.例如,
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
虽然在命令行上输入上述内容看起来很费力,但我通常在我的 makefile 中使用这个想法.
While the above looks like a lot of effort to type out on the command line, I generally use this idea in my makefiles.
仅供参考,等效的英特尔 fortran 编译器标志是 -I
和 -module
.本质上,ifort 将 -J
选项替换为 -module
.注意module后面有空格,J后面没有.
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.
这篇关于指定 gfortran 应在其中查找模块的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!