Warm tip: This article is reproduced from serverfault.com, please click

dependencies-Fortran Makefile中目标文件“ * .o”的自动排序

(dependencies - Automatic Ordering of Object Files "*.o" in a Fortran Makefile)

发布于 2014-09-06 22:17:30

这里有两个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.omain_mod.mod然后main_mod2.f90将成功编译。这意味着目标文件的排列必须是:

main_mod.o main_mod2.o

现在,问题是,在一般情况下,当我使用通配符创建目标文件列表时,如何执行目标文件的正确排列?

Questioner
argasm
Viewed
11
Yossarian 2014-09-12 15:21:54

尽管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的列表