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 a 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 (Linux and Mac OS) or /I compiler (Windows) 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 -module path (Linux and Mac OS) or /module:path (Windows) option to specify the directory in which to create the module files. If you don't use this option, module files are created in the current directory.
Directories are searched for .mod files in this order:
Directory of the source file that contains the USE statement
Directories specified by the -module path (Linux and Mac OS) or /module:path (Windows) option
Current working directory
Directories specified by the -Idir (Linux and Mac OS) or /include (Windows) option
Directories specified with the FPATH (Linux and Mac OS) or INCLUDE (Windows) environment variable
Standard system directories
You need to make sure that the module files are created before they are referenced by another program or subprogram.
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:
test.mod
payroll.mod
a.o (Linux and Mac OS) or a.obj (Windows)
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 program mod_def.f90 which contains a module defined as follows:
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 Mac OS) or mod_def.obj (Windows) and also the .mod file definedmod.mod, all in the current directory.
If you need to use the .mod file in another directory, do the following:
file: use_mod_def.f90
program usemod
use definedmod
.
.
.
end program
To compile the above program, use the -I (Linux) or /I (Windows) option to specify the path to search and locate the definedmod.mod file.