Using Module (.mod) Files

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 USE statement. Typical applications of modules are the specification of global data or the specification of a derived type and its associated operations.

Some programs require modules located in multiple directories. You can use the -Idir option when you compile the program to locate the .mod files that should be included in the program.

You can use the -module path option to specify the directory where to create the module files. This path is also used to locate module files. If you don't use this option, module files are created in the current directory.

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:

module test

integer:: a

contains

  subroutine f()

  end subroutine

end module test

 

module payroll

.

.

.

end module payroll

This compiler command:

ifort -c a.f90

generates the following files:

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

If the program does not contain a module, no .mod file is generated. For example, test2.f90 does not contain any modules. This compiler command:

ifort -c test2.f90

produces just an object file, test2.o.

For another example, assume that file1.f90 contains one or more modules and file2.f90 contains one or more program units that access these modules with the USE statement. The sources can be compiled and linked by this command:

ifort file1.f90 file2.f90

Working with Multi-Directory Module Files

For an example of managing modules when the .mod files could be produced in different directories, assume that the program mod_def.f90 resides in directory /usr/yourdir/test/t, and this program contains a module defined as follows:

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

The compiler command:

ifort -c mod_def.f90

produces two files: mod_def.o and definedmod.mod in directory /usr/yourdir/test/t.

If you need to use the above .mod file in another directory, for example, in directory /usr/yourdir/test/t2, where the program usemod uses the definedmod.mod file, do the following:

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

To compile the above program, use this command:

ifort -c use_mod_def.f90 -I/usr/yourdir/test/t

where the -Idir option provides the compiler with the path to search and locate the definedmod.mod file.

Parallel Invocation with a makefile

The programs containing module definitions support parallel invocation using  a makefile. Consider the following code:

test1.f90

module m1

.

.

.

end module

test2.f90

subroutine s2()

use m1

.

.

.

end subroutine

test3.f90

subroutine s3()

use m1

.

.

.

end subroutine

The makefile to compile the above code looks like this:

m1.mod: test1.o

test1.o: test1.f90

ifort -c test1.f90

test2.o: m1.mod test2.f90

ifort -c test2.f90

test3.o: m1.mod test3.f90

ifort -c test3.f90