< Table Of Contents

IntelĀ® Fortran Compiler 17.0 Developer Guide and Reference

Using Module (.mod) Files

One way to reduce potential confusion when you use the same source code in several projects is to organize the routines into modules. A module (.mod file) is a type of program unit that contains specifications of such entities as data objects, parameters, structures, procedures, and operators. These precompiled specifications and definitions can be used by one or more program units. Partial or complete access to the module entities is provided by the program's USE statement. Typical applications of modules are the specification of global data or the specification of a derived type and its associated operations.

Modules are excellent ways to organize programs. You can set up separate modules for:

Some programs require modules located in multiple directories. You can use the I compiler option when you compile the program to specify the location of the .mod files that should be included in the program.

You can use the modulepath compiler option to specify the directory in which to create the module files. If you do not use this option, module files are created in the current directory.

Directories are searched for .mod files in this order:

  1. Directory of the source file that contains the USE statement.

  2. Directories specified by the modulepath compiler option.

  3. Current working directory.

  4. Directories specified by the -Idir (Linux* and OS X*) or /include (Windows*) option.

  5. Directories specified with the CPATH or INCLUDE environment variable.

  6. Standard system directories.

You need to make sure that the module files are created before they are referenced by another program or subprogram.

Compiling Programs with Modules

If a file being compiled has one or more modules defined in it, the compiler generates one or more .mod files.

For example, a file a.f90 contains modules defined as follows:

a.f90 Module

module test
integer:: a
contains
  subroutine f()
  end subroutine
end module test 
module payroll 
  ...
end module payroll 

This compiler command:

ifort -ca.f90

generates the following files:

The .mod files contain the necessary information regarding the modules that have been defined in the program a.f90.

The following example uses the program mod_def.f90 which contains a module defined as follows:

mod_def.f90 Module

file: mod_def.f90
module definedmod
  ...
end module

Compile the program as follows:

ifort -c mod_def.f90

This produces the object files mod_def.o (Linux* and OS X*) or mod_def.obj (Windows*) and also the .mod file definedmod.mod, all in the current directory.

Using .mod files from another directory

file: use_mod_def.f90
program usemod
use definedmod 
  ...
end program

To compile the above program, use the I compiler option to specify the path to search and locate the definedmod.mod file.

See Also