0

I have something like this:

Module ModA
    contains
    subroutine FooA()
       ....
    end subroutine
    subroutine FooB()
       ....
    end subroutine
end mofule ModA

can I split the two subroutines each in a separate file and still both belongs to the same module.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
  • possible duplicate of [Single fortran module in multiple files](http://stackoverflow.com/questions/11967053/single-fortran-module-in-multiple-files) – Alexander Vogt Jan 25 '15 at 12:11
  • This issue has been treated on SO multiple times... See here: http://stackoverflow.com/questions/12423637/fortran-module-procedure-outside-module or here: http://stackoverflow.com/questions/22825841/what-are-submodules-and-how-are-they-used – Alexander Vogt Jan 25 '15 at 12:12
  • but there was no clear answer like the one I got. thanks anyway I checked those questions yesterday and did not get any useful results. – Mahmoud Fayez Jan 25 '15 at 13:36

1 Answers1

6

Yes. You have two options.

The first is to simply INCLUDE the file with the source of a subroutine in the module subprogram part.

! Module.f90
MODULE ModA
CONTAINS
  INCLUDE 'FooA.f90'
  INCLUDE 'FooB.f90'
END MODULE ModA

! FooA.f90
SUBROUTINE FooA()
!...
END SUBROUTINE FooA

! FooB.f90
SUBROUTINE FooB()
!...
END SUBROUTINE FooB

The second option is to move each subroutine into its own submodule.

MODULE ModA
  INTERFACE
    MODULE SUBROUTINE FooA()
    END MODULE SUBROUTINE FooA
    MODULE SUBROUTINE FooB()
    END MODULE SUBROUTINE FooB()
  END INTERFACE
END MODULE ModA

! FooA.f90 (or whatever name you prefer)
SUBMODULE (ModA) FooA_submodule
CONTAINS
  MODULE PROCEDURE FooA
  !...
  END PROCEDURE FooA
END SUBMODULE FooA_submodule

! FooB.f90 (or whatever name you prefer).
SUBMODULE (ModA) FooB_submodule
CONTAINS
  MODULE PROCEDURE FooB
  !...
  END MODULE PROCEDURE FooB
END SUBMODULE FooB_submodule

But you will need to use a Fortran compiler that supports the Fortran 2008 submodule feature for this option.

IanH
  • 21,026
  • 2
  • 37
  • 59
  • and I will use FooA by typying "using Fooa_submodule", right? – Mahmoud Fayez Jan 25 '15 at 09:53
  • 1
    In the submodule case (noting again that this requires compiler support) `USE ModA` is all that is required to make FooA and FooB accessible in another scope. – IanH Jan 25 '15 at 19:44
  • in the fist option you cannot have the `FooA.f90` and `FooB.f90` files added to the project because they are going to be compiled. There are going to be two versions of each function, one outside the module and one inside the module. If you don't include all sources to the project, then maintenance becomes a problem. So only reserve the first option for files that are common to ALL projects and are outside the scope of each project. Something like common declarations etc. – JAlex Nov 02 '21 at 20:53
  • You don't compile (separately) the included files, you compile the files that do the including. Often people might use a different file extension for included files (e.g. `i90`). How things are best set up for whatever build and source management systems you are using, depends on those specific build and source management systems (as does the meaning of the word "project" in your comment - that is not a Fortran language term and doesn't appear in question or answer). – IanH Nov 03 '21 at 01:26