Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile Fortran FUNCTION/SUBROUTINE in separate files into a single MODULE

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?

like image 580
QuantumDot Avatar asked Sep 16 '25 16:09

QuantumDot


1 Answers

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"
like image 128
Alexander Vogt Avatar answered Sep 18 '25 06:09

Alexander Vogt