How to compile Fortran FUNCTION/SUBROUTINE in separate files into a single MODULE
Solution 1
You can simply use include
to include another file of Fortran source code:
!mystuff.f90
MODULE mymodule
IMPLICIT NONE
CONTAINS
include 'mysubrtn1.f90'
include 'mysubrtn2.f90'
!lots more functions and subroutines
END MODULE
From here:
The INCLUDE statement directs the compiler to stop reading statements from the current file and read statements in an included file or text
So you can see that the resulting module will still contain both subroutines.
An alternative that achieves the same thing is to use a pre-processor directive, if your compiler supports it:
#include "filename"
Solution 2
For readability, it is nice to separate a large module into more manageable chunks. Each of the smaller modules may be compiled individually, and used in another "master" module which is then used in the main program. The main benefit of this approach is that you can have a variety of very general modules, and pick only the procedures/data that are useful at the moment. For example:
module mod_1
implicit none
subroutine proc_1
! ...
end subroutine
! other procedures, etc.
end module mod_1
And so on, for each of your separate modules. Then collect them in a single module.
module collection
use mod_1, only: proc_1 ! pick & choose what to use
use mod_2
use mod_3
! ...
end module collection
program main
use collection
implicit none
! ...
end program main
When you compile the main program, you can link to each of the necessary object files, or even combine them into a single archive/library and just link to that.

QuantumDot
As time goes on, I grow more disillusioned with quantum field theory.
Updated on June 04, 2022Comments
-
QuantumDot 7 months
Usually, when I write a collection of Fortran functions, I put them into a
MODULE
like this:!mystuff.f90 MODULE mymodule IMPLICIT NONE CONTAINS SUBROUTINE mysubroutine1(a,b) !code END SUBROUTINE SUBROUTINE mysubroutine2(a,b) !code END SUBROUTINE !lots more functions and subroutines END MODULE
and I successfully compile it like this
gfortran -c mystuff.f90
. This createsmystuff.o
which I can use in my main program.However, the number and individual sizes of functions/subroutines in my
MODULE
have become so huge, that I really need to split up this up into different files.!mysubrtn1.f90 SUBROUTINE mysubroutine1(a,b) !code END SUBROUTINE
and
! mysubrtn2.f90 SUBROUTINE mysubroutine2(a,b) !code END SUBROUTINE
and so forth...
But I'd still like to keep all these functions inside a single
MODULE
. How can I tell the compiler to compile the functions inmysubrtn1.f90
,mysubrtn2.f90
, ... such that it produces a single module in a.o
file? -
QuantumDot over 5 yearsVery nice! Quick followup question: In each of my functions I have
USE xyz
. Obviously, I can take that line out from each file and put it at the beginning ofmystuff.f90
. But is there any harm in leaving it in every file and also at the beginning ofmystuff.f90
? -
Alexander Vogt over 5 yearsDo you mean what the difference is between the scope of the module or the scope of a function/subroutine?
-
QuantumDot over 5 yearsI think I understand that difference. Putting
USE xyz
at the top ofmystuff.f90
will makexyz
available to all functions/subroutines in that module. I just have a habit of also including it inside every one of those functions/subroutines. (I think this makes no difference); I'm just wondering if I'm confusing the compiler if I haveUSE xyz
at the beginning ofmystuff.f90
and also inside every one of the functions/subroutines. Do correct me if I'm wrong. -
QuantumDot over 5 yearsOk, so another strategy is to define a separate module for each my functions, and then put those modules into a bigger module. Did I understand you correctly?
-
Matt P over 5 yearsThat's right. I just expanded a bit on the original answer. Athough, I think that @Alexander Vogt's answer accomplishes the same task with fewer compilation steps!
-
arclight over 5 yearsAre there problems with using this technique with
make
, since the changes to theinclude
d files won't trigger a rebuild on the parent file? Would submodules help with this? -
swimfar over 2 yearsThat's a good point, @arclight. To solve that problem I think you would just need to include your subroutine files as dependencies of the
mystuff.o
module.