这里有两个Fortran90文件和一个makefile:
文件 main_mod.f90的内容:
module main_mod
contains
subroutine add(a, b)
implicit none
integer, intent(in) :: a, b
print *, (a+b)
end subroutine add
end module main_mod
文件 main_mod2.f90的内容
module main_mod2
use main_mod
contains
subroutine add2(a, b)
implicit none
integer, intent(in) :: a, b
call add(a, b)
end subroutine add2
end module main_mod2
在makefile中,我自动从当前目录中生成“ .o”文件列表:
F90 = /usr/bin/gfortran
COMPFLAGS = -c
%.o: %.f90
$(F90) $(COMPFLAGS) $*.f90
all: $(patsubst %.f90,%.o,$(wildcard *.f90))
当我制作项目时,我的制作文件中的通配符语句会生成目标文件列表,例如:
main_mod2.o main_mod.o
然后编译失败,因为首先需要编译文件main_mod.f90,这将给我们main_mod2.f90中使用的main_mod.o和main_mod.mod。然后main_mod2.f90将成功编译。这意味着目标文件的排列必须是:
main_mod.o main_mod2.o
现在,问题是,在一般情况下,当我使用通配符创建目标文件列表时,如何执行目标文件的正确排列?
尽管gcc确实具有-M
用于C / C ++文件执行此操作的标志和相关标志,但不幸的是,它们不适用于gfortran。实际上,这是可能的,但前提是你已经知道依赖关系。因此,你将需要一个外部程序来生成依赖项。
在我的项目中,我使用此python脚本,并将以下内容添加到我的makefile中:
# Script to generate the dependencies
MAKEDEPEND=/path/to/fort_depend.py
# $(DEP_FILE) is a .dep file generated by fort_depend.py
DEP_FILE = my_project.dep
# Source files to compile
OBJECTS = mod_file1.f90 \
mod_file2.f90
# Make sure everything depends on the .dep file
all: $(actual_executable) $(DEP_FILE)
# Make dependencies
.PHONY: depend
depend: $(DEP_FILE)
# The .dep file depends on the source files, so it automatically gets updated
# when you change your source
$(DEP_FILE): $(OBJECTS)
@echo "Making dependencies!"
cd $(SRCPATH) && $(MAKEDEPEND) -w -o /path/to/$(DEP_FILE) -f $(OBJECTS)
include $(DEP_FILE)
fort_depend.py
基本上只是列出USE
给定文件中所有模块d的列表。
谢谢您这个不可思议的答案!我现在唯一的问题是python脚本始终会给我以下错误:追溯(最近一次调用最近):在<module>中的args.D文件中的“ fort_depend.py”文件第161行,D:TypeError:' NoneType'对象不可迭代。您之前有类似的错误吗?我该如何解决?再次感谢。
好的,我想我已经修复了该错误。您可以重新尝试最新的更改吗?
fort_depend.py
允许您为我可能难以置信的特定用例指定预处理器宏。我还没有找到其他的Fortran依赖项生成器,它允许您执行此操作。我运行“ ./fort_depend.py -f mod_file1.f90 mod_file2.f90”将文件解析为python脚本。但是,在字典中查找时出了点问题。这是错误消息:追溯(最近一次呼叫最近):文件“ ./fort_depend.py”,行172,在<module>中运行(文件= args.files,详细= args.verbose,覆盖= args.overwrite,宏=宏,输出=输出)在运行mod2fil = file_objs_to_mod_dict(FIL_OBJS = l)时,文件“ ./fort_depend.py”,第9行TypeError:file_objs_to_mod_dict()获得了意外的关键字参数'FIL_OBJS'。如果您解决此问题,我将很高兴。谢谢!
另外,如果您认为这解决了您的问题,则可以单击接受(如果愿意,也可以投票):)