问题描述
这里有两个Fortran90文件和一个makefile文件:
Here I have two Fortran90 files and a 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 / p>
contents of file 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文件列表:
and in makefile, I automatically generate a list of ".o" files from current directory:
F90 = /usr/bin/gfortran
COMPFLAGS = -c
%.o: %.f90
$(F90) $(COMPFLAGS) $*.f90
all: $(patsubst %.f90,%.o,$(wildcard *.f90))
当我做项目时,我的make文件中的通配符生成一个目录文件列表,如:
when I make the project, the wildcard statement in my make file generates a list of object files like:
main_mod2.o main_mod.o
然后编译失败,因为首先,文件 main_mod.f90 需要编译,这将给我们在main_mod2.f90 中使用的 main_mod.o 和 main_mod.mod 。那么main_mod2.f90 将被编译成功。这意味着对象文件的排列必须是:
and then the compilation fails because first, the file main_mod.f90 needs be compiled which would give us main_mod.o and main_mod.mod used in main_mod2.f90. Then main_mod2.f90 would be compiled successfully. That means the permutation of object files must be:
main_mod.o main_mod2.o
现在,一般情况下,当我使用通配符创建对象文件列表时,如何执行对象文件的正确排列? / p>
Now, the question is, in general case when I create the list of object files using wildcard, how can I enforce correct permutation of object files?
推荐答案
虽然gcc确实有 -M
和相关标志与C / C ++文件,他们不幸的是不能与gfortran一起工作。其实这是可能的,但只有当你已经知道这些依赖性的时候。
因此,您将需要一个外部程序来生成依赖关系。
While gcc does have -M
and related flags for doing exactly this with C/C++ files, they unfortunately do not work with gfortran. Actually, it is possible, but only if you already know the dependencies.Therefore you will need an external program to generate your dependencies.
在我的项目中,我使用,并将以下内容添加到我的makefile中:
In my projects, I use this python script, and add the following to my 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给定文件。
fort_depend.py
basically just makes a list of all the modules USE
d in a given file.
这篇关于自动排序Obejct文件“*。o”在Fortran Makefile中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!