Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

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

Here I have two Fortran90 files and a makefile:

Contents of file 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

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

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))

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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 basically just makes a list of all the modules USEd in a given file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...