在Fortran Makefile中自动排序对象文件"*.o"

arg*_*asm 9 dependencies fortran makefile wildcard

这里我有两个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
Run Code Online (Sandbox Code Playgroud)

文件 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
Run Code Online (Sandbox Code Playgroud)

makefile中,我自动生成当前目录中的".o"文件列表:

F90 = /usr/bin/gfortran
COMPFLAGS    =  -c
%.o: %.f90
        $(F90) $(COMPFLAGS) $*.f90

all: $(patsubst %.f90,%.o,$(wildcard *.f90))
Run Code Online (Sandbox Code Playgroud)

当我创建项目时,make文件中的wildcard语句生成一个目标文件列表,如:

main_mod2.o main_mod.o
Run Code Online (Sandbox Code Playgroud)

然后编译失败,因为首先,需要编译main_mod.f90文件,这将给我们main_mod2.f90中使用的main_mod.omain_mod.mod.然后main_mod2.f90将成功编译.这意味着目标文件的排列必须是:

main_mod.o main_mod2.o
Run Code Online (Sandbox Code Playgroud)

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

Yos*_*ian 9

虽然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)
Run Code Online (Sandbox Code Playgroud)

fort_depend.py基本上只是列出USE给定文件中的所有模块d.