Reputation: 435
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 creates mystuff.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 in mysubrtn1.f90
, mysubrtn2.f90
, ... such that it produces a single module in a .o
file?
Upvotes: 3
Views: 16024
Reputation: 2347
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.
Upvotes: 2
Reputation: 18098
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"
Upvotes: 9