Fortran Wiki
Compiler .MOD files

Joe Krahn:

It seems to me that binary .MOD files are an unnecessary hassle. The compiler could instead parse the module source directly instead of storing it in and compiler and version dependent data file.

The MOD file is similar to a pre-compiled header in C. However, they are only used for optimization, and need not be distributed. A Fortran compiler could easily use the module source as the interface spec, allowing for procedure bodies to be deleted. The stripped module source file can then be the actual interface specification, and could include useful comments as in C headers.

The only caveat to this approach is that module interfaces are referenced by the module name, and not the file name. There would need to be a standard convention for naming the stripped module “header” files.

A better mechanism is for compilers to provide a feature to generate mod files from the module source without actually compiling the source code, and allowing for stripped procedure bodies. This would work more like C pre-compiled headers, where library .mod files are for optimizing the compile process, and do not need to be distributed with the library.

The .mod file also ensures a matching compiler version. It is better to check the object files for compatibility. This should be easy to implement for shared libraries, where the mod-file distribution is most problematic.


Submodules make it possible for a parent module to define all procedure interfaces with no source code. This form of module file could be distributed unmodified as a library API.


Also see Library distribution? for related discussion on .mod files.