Fortran submodule file naming

In many applications, a Fortran submodule is defined in a different file than the Fortran module that uses it. A slight problem arises for build generators since each Fortran compiler treats Fortran submodule interface file naming distinctively. Fortran module interface files are somewhat of a dual to the header files of C / C++, except that module files are automatically generated and generally not human-readable. This non-standard Fortran submodule file naming causes breakage to build generation systems such as CMake and Meson that have not yet updated their per-compiler file naming definitions.


For the benefit of compiler vendors and build generators developers (both of whom read this blog) here is a brief taxonomy of file naming conventions for Fortran submodules. In all cases, we assume a two-file program as follows:

file basic.f90

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

  module subroutine hello(pi,tau)
    real, intent(in) :: pi
    real, intent(out) :: tau
  end subroutine hello
end interface
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
module procedure hello
  tau = 2*pi
end procedure hello
end submodule hi

Note that the order of the commands for each compiler matters–the module must be built before the submodule, to generate the necessary module interface files BEFORE compiling the submodule. To simplify the output, we only include commands for Gfortran–substiute the desired compiler name. Of course, each compiler creates corresponding basic.o and basic_sub.o object files as usual, omitted here for clarity.

  1. gfortran -c basic.90 creates files: basic.o demo.mod demo.smod.
  2. gfortran -c basic_sub.f90 -o basic_sub.o creates files: basic_sub.o demo@hi.smod.
  3. gfortran basic.o basic_sub.o -o basic creates executable basic

Here is a table of the module interface files generated by each compiler. “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
gfortran demo.mod demo.smod demo@hi.smod
flang demo.mod demo-hi.mod
pgf90 demo.mod demo-hi.mod
ifort demo.mod demo@hi.smod
xlf2008 demo.mod demo_hi.smod


Tags: ,


Written by Michael Hirsch, Ph.D. //