The mysterious nature of Fortran 90 modules

frostbyyte picture frostbyyte · Sep 6, 2012 · Viewed 12k times · Source

Fortran 90 modules are evanescent creatures. I was using a (singular) module for a while with some success (compiling using Intel Visual Fortran and Visual Studio 2010). Then I wrote another module and tried to USE it in another function, before receiving this error:

 error #7002: Error in opening the compiled module file.  Check INCLUDE paths.

So I deleted the offending module. But now I receive the same error after when trying to access my original module!

How can I locate these mysterious creatures? Why does one module work but not two? I'm assuming that I need to delete and recompile them, or tell the compiler to include them somehow. I know the file locations of the source code but not where they compile to.

Answer

IanH picture IanH · Sep 7, 2012

For that specific processor (many other Fortran processors have similar characteristics, but the details differ):

  • When a module is compiled successfully, the compiler generates a .mod file (and perhaps an .obj file) that contains information about the entities provided by the module. It is this mod file that the error message you quote is referring to. The compiler requires this mod file when it encounters a USE statement for the module while compiling other source. (The obj file is used in the link stage.)

  • Hence, before a module is USE'd, the compiler must at some time have compiled the source code for the module. That means that the module's source code (MODULE...END MODULE) must have appeared earlier in the source file prior to the USE statement, or must have been in a separate file that was compiled prior to the source file with the USE statement.

  • When compiling using an Intel Fortran project within Visual Studio, the build environment will automatically attempt to arrange an appropriate compilation order for the source files within a project. When compiling using the ifort command from the command line the programmer is responsible for managing the compilation order.

  • The directory that receives the generated mod files is specified by the first /module command line option given to the compiler. In Visual Studio this option is set using the Fortran > Output Files > Module Path property. By default, a Fortran project in Visual Studio has this property set to be the name of the current configuration, hence the mod files appear in a child directory of the project called Debug or Release. In the absence of a /module command line option the mod files appear in the current directory.

  • Directories specified by the /module command line option (or the equivalent Visual Studio property) are also used in the search for mod files. In addition, directories specified by the /I command line option (in Visual Studio, Fortran > General > Additional Include Directories) are searched.

It is not clear from your question as to how you have distributed your modules amongst your source files, whether you have a single Visual Studio project or multiple projects etc. If you are only dealing with a single project then typically all that is required is to add all the Fortran files to the Source files for the project, and the default settings should "work". Errors in finding mod files may be because:

  • the associated source for the module isn't in one of the source files for the project;

  • compilation of the source for a module failed for some other reason (are other errors listed earlier in the build sequence?)

  • a module is defined after its use in a particular source file;

  • there are circular dependencies between modules (module A uses module B which uses A or similar - this is not allowed by the rules of the language);

  • some source construct that confuses the automatic determination of build order (older versions of the build system were confused by the F2003 form of use statement with the double colon, plus it is possible to obfuscate USE statements such that the build system fails to identify them) but these aspects are pretty obscure.

With multiple Fortran projects in Visual Studio there may be a need to modify the module search directories for dependent projects, such that they can find the mod files compiled by previous projects in the project dependency tree. Later versions of Intel Fortran handle this aspect automatically too, if the inter-project dependency settings in Visual Studio are correct.