Fortran module file format

The Fortran standard does not define a .mod file format, and every compiler has a unique incompatible format. This means that .mod files are not portable between different compilers or even different versions of the same compiler.

The per-compiler examples below assume Fortran source file “example.f90”:

module dummy

real, parameter :: pi = 4*atan(1.0)

contains

real function tau()
  tau = 2*pi
end function tau

end module dummy

Flang-new (LLVM)

The flang-f18 or flang-new LLVM Fortran .mod files generated are legal Fortran syntax – they are text files. The .mod format gives the version number.

Build:

gfortran -c example.f90

This generates Fortran module file “dummy.mod” and object file “example.o”.

Get the .mod file header:

head -n1 dummy.mod

The output starts like:

!mod$ v1

GNU Fortran (GFortran)

The GFortran header version is defined in module.cc as variable “MOD_VERSION”. GNU Fortran “gfortran” .mod files are GZIP and is not documented

GCC version module file version
up to 4.3.2 unversioned
4.4 0
4.5.1 4
4.6.3 6
4.7.0pre 8
4.7.1 9
4.8.1 10
4.9.2 12
5.1.0 14
8.x - 14.x 15

Build:

gfortran -c example.f90

This generates Fortran module file “dummy.mod” and object file “example.o”.

Extract the .mod file header like:

gunzip -c dummy.mod | head -n1

The output starts like:

GFORTRAN module version ‘15’ created from …

If you get:

gzip: not in gzip format

the .mod file is probably from another compiler vendor e.g. Intel oneAPI.

Intel oneAPI (ifx)

Intel oneAPI .mod files are a proprietary binary format. It is possible to determine the version of the .mod file by using od to look at the first 2 bytes of the .mod file.

od -N4 -d dummy.mod

The first number is like “13” and is the module format version. This version may change over time as oneAPI internals change. The second number is the update version, which is fixed at “1”.

NVHPC and other old Flang compilers

NVIDIA HPC SDK (NVHPC), AOCC, and other old Flang compilers generate .mod files that are text files. The format is different than flang-new.

Create the .mod file like:

nvfortran -c example.f90

# or

flang -c example.f90

generates a text file, beginning with the version number.

head -n1 dummy.mod

the output is like:

V34 :0x24 dummy

Cray Fortran

By default, Cray Fortran stores uppercase DUMMY.mod filenames. This can be made lowercase witht the ftn -ef flag. The Cray Fortran .mod format is proprietary, but the version number might be seen like:

ftn -c example.f90

head -n2 DUMMY.mod

Related: Fortran submodule file naming