Notes on gfortran


Almost every Fortran compiler has a debugging option for run-time array-bounds checking, which does useful checks at the expense of often considerable performance loss, so it should not be used in production versions of code. Almost every Fortran compiler enables this with the option -C. With gfortran, -C affects how the preprocessor treats certain comments. The option for array bounds checking is -fcheck=bounds.

Module File Versioning

gfortran is an increasingly mature and useful member of the Gnu Compiler Collection. Like most other compilers, it is fairly conservative about changing its ABI: object files and libraries compiled with one version of gfortran generally work fine with any newer version.

However, it is far from conservative about changing the format of the module files it produces. It changes them on most major releases. An incomplete list of compiler version numbers against module file version numbers is:


This becomes an issue if one attempts to have a central installation of a library and associated module files used by a selection of clients using different versions of gfortran. The C or C++ analogue of library with associated header file will work fine, as the header file may depend on the library version, but not on the compiler version. With gfortran one is liable to see errors such as:


  use mpi
Fatal Error: Cannot read module file 'mpi.mod' opened at (1),
because it was created by a different version of GNU Fortran

One needs to ensure that the module file is created with the same version of the compiler that is being used for the compilation which includes it. This can make installing Fortran libraries on a system with multiple versions of gfortran hard. Whilst one library file is likely to suffice, it is less likely that a single module file will suit all versions.

To determine the version number of a gfortran module file, for early versions the .mod file is simply a text file, and

$ head -1 mpi.mod
GFORTRAN module version '10' created from mpi.f90

suffices. For later versions, the .mod file is gzipped, so one needs

$ zcat mpi.mod | head -1
GFORTRAN module version '14' created from mpi.f90