Fortran submodule file naming

A Fortran submodule may be defined in the same file or a different file than the Fortran module that uses the submodule. Meta-build systems such as CMake and Meson are aware that each Fortran compiler has distinct Fortran submodule naming conventions. Fortran module and submodule interface files are like generated header files.

The order of the commands for each compiler is significant and handled by the meta build system. The Fortran module must be built before the Fortran submodule to generate the necessary module interface files BEFORE compiling the submodule. Each compiler creates corresponding basic.o and basic_sub.o object files.

  1. gfortran -c basic.90 creates object and module files: “basic.o” “demo.mod” “demo.smod”
  2. gfortran -c basic_sub.f90 -o basic_sub.o creates object and submodule files: “basic_sub.o” “demo@hi.smod
  3. gfortran basic.o basic_sub.o -o basic creates executable “basic”

“module” are the files generated by step #1, building the file containing the Fortran module. “submodule” are the files generated by step #2, building the containing the Fortran submodule.

Compiler module files submodule files
GCC gfortran demo.mod demo.smod demo@hi.smod
LLVM flang demo.mod demo-hi.mod
Nvidia HPC nvfortran demo.mod demo-hi.mod
Intel ifx demo.mod demo@hi.smod
IBM xlf demo.mod demo_hi.smod
Cray ftn DEMO.mod HI.mod

GCC Gfortran module and submodule naming convention is defined in module.cc by definintions “MODULE_EXTENSION” and “SUBMODULE_EXTENSION”. LLVM Flang module and submodule naming convention is defined in Semantics.

Example program

The table above is derived from the two-file example program:

file basic.f90

module demo
real, parameter :: pi = 4.*atan(1.)
real :: tau

interface
  module subroutine hello(pi,tau)
    real, intent(in) :: pi
    real, intent(out) :: tau
  end subroutine hello
end interface
contains
end module demo

program sm
use demo
call hello(pi, tau)
print *,'pi=',pi, 'tau=', tau
end program

file basic_sub.f90

submodule (demo) hi
contains
module procedure hello
  tau = 2*pi
end procedure hello
end submodule hi

Related: