本文介绍了对象文件"* .o"的自动排序.在Fortran Makefile中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有两个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

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_mod.o main_mod.mod main_mod2.f90 中使用.这样, 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

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

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.

在我的项目中,我使用此python脚本,并将以下内容添加到我的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 USEd in a given file.

这篇关于对象文件"* .o"的自动排序.在Fortran Makefile中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:32