how to compile multi-folder Fortran Project having interfaces, modules and subroutines

后端 未结 2 448
傲寒
傲寒 2020-12-10 07:24

I am new to Fortran. I am working on a research project where I am using an open source project that has several files distributed in multiple folders. i found the dependenc

相关标签:
2条回答
  • 2020-12-10 08:23

    You can do a Makefile which looks like that

    F90=gfortran
    FFLAGS = -O0
    VPATH = modules:interfaces:subroutines:
    MODOBJ = module1.o module2.o ...
    
    your_executable: $(MODOBJ) main.o
            $(F90) main.o -o your_executable
    %.o:%.f90
            $(F90) $(FFLAGS) -c $^ -o $@
    

    VPATH is the paths of the directories where your Makefile will look for source files, so if you compile your source in the root directory of modules/, interfaces/ and subroutines/, you just have to set up VPATH like that.

    If you have many objects and you don't want to write everything by hand, you can retrieve them using the following trick

    F90 = gfortran
    FFLAGS = -O0
    VPATH = modules:interfaces:subroutines
    SRCOBJ = $(wildcard modules/*f90)
    MODOBJ = $(SRCOBJ:.f90=.o)
    
    your_executable: $(MODOBJ) main.o
            $(F90) main.o -o your_executable
    %.o:%.f90
            $(F90) $(FFLAGS) -c $^ -o $@
    

    The wildcard command in a Makefile allows you to use a joker *; then you just have to say that in the strings you will retrieve in $(SRCOBJ), you want to substitute .f90 by .o to get the filenames of your modules.

    0 讨论(0)
  • 2020-12-10 08:26

    You can create your Makefiles as usual. The biggest problem should be the .mod files. The easiest solution to this problem is to create a separate folder, where these files are stored and searched for.

    This can be achieved with the -J and the -module flags for gfortran and ifort, respectively.

    0 讨论(0)
提交回复
热议问题