2

以下是我的项目的目录结构:

                       expt-main
                       ---------
                       Makefile_main
                    /             \
         subdir-1                    subdir-2
         --------                    --------
         Makefile_1                  Makefile_2
         mod_codeA.f90               mod_code1.f90
         mod_codeB.f90               mod_code2.f90
         mod_codeC.f90               mod_code3.f90

Makefile_main:

export

SHELL = /bin/sh

F90     = mpxlf95

SRCDIRS = $(subdir-1) $(subdir-2)

all:
        @for DIR in ${SRCDIRS} ;

          do \
            back=`pwd`; \
            cd $$DIR ;\
            $(MAKE) ; status=$$? ; \
            if [ $$status != 0 ] ; then \
              echo "Exit status fro make was $$status" ; exit $$status ; \
            fi ; \
            cd $$back ; \
         done

-------------------------------------------------------------------------------

Makefile-1:

%.o: %.f90
        $(F90) $(F90FLAGS) -I$(subdir-2) -c $<

mod_codeA.o: mod_codeC.o $(subdir-2)/mod_code2.o

-------------------------------------------------------------------------------

Makefile-2:

PROG = $(exec)

subdir-1_objs = $(subdir-1)/mod_codeA.o mod_codeB.o mod_codeC.o

all: $(PROG)

$(PROG): $(subdir-2_objs) $(subdir-1_objs) -o $@ $(subdir-2_objs) $(subdir-1_objs)

---------------------------------------------------------------------------------

-

我编写了 Makefile_main 以便它首先编译 subdir-1 中的代码(模块),然后编译 subdir-2 中的代码(模块),最后编译为可执行文件。问题:subdir-1 中的模块使用 subdir-2 中的模块,并且以类似的方式,subdir-2 中的模块使用 subdir-1 中的模块。我的制作失败了,因为正在使用的模块在其他目录中。如何编写一个 makefile 来解决这个问题,即在 subdir-1 中编译模块时,每当遇到需要从 subdir-2 获取目标文件时,它应该切换到 subdir-2 编译必要的模块并返回返回 subdir-1 进行进一步操作?

4

2 回答 2

2

If modules in different subdirectories need each other as you say, then this is not a good use of recursive Make.

Do away with Makefile-1 and Makefile-2, and let Makefile_main do all the work. (I can't tell you specifically how to change Makefile-main, since I don't do Fortran, I don't understand Makefile-2, and I don't see any dependency of modules in subdir-2 upon those in subdir-1).

于 2012-10-15T13:27:29.167 回答
1

If you want to stick to this directory layout and still keep three separated Makefiles, then you can use compiler flags to instruct the FORTRAN compiler to put module files into a common directory of your choice.

For instance using:

$ gfortran --version
GNU Fortran (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.

you can use -I and -J flags to instruct the compiler on:

  1. where to search for module files (.mod)
  2. where to put generated module files

That said I think that the suggestion given by Beta to join the Makefiles makes a lot of sense. To know some of the reasons why you should do that you can read this paper.

Finally, as your project seems not to be very large at this stage, I also suggest to take into consideration CMake as a build system, as it possibly provides a more convenient way of specifying dependencies between targets (as well as many other things).

于 2012-10-16T10:48:01.260 回答