# Use Module (.mod) Files

A way to reduce potential confusion when you use the same source code in several projects is to organize the routines into modules and submodules.

A module 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.

You can set up separate modules for:

- Commonly used routines

- Data definitions specific to certain operating systems

- System-dependent language extensions

Submodules let you separate the interface of procedures from their implementation, making it easier to build applications and perform maintenance.

To use a module, its source must first be compiled into a .mod file. The name of the .mod file is the module name, not the source file name. Similarly, submodules are compiled into .smod files; their parent modules or submodules must be compiled first. Compiling a module or submodule also creates an object file (.o or .obj) that must be included when linking the application.

When compiling a source that has a USE statement containing a module, the compiler looks for the corresponding .mod file in the same place it looks for include files. Submodule .smod files are used only when a child submodule is compiled.

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

Intrinsic modules, defined by the Fortran standard, are supplied in a system directory alongside the compiler binaries and libraries. These have a .modintr file type that is searched only when the Fortran source includes USE, INTRINSIC.

You can use compiler option module path 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.

When the compiler is looking for .mod and .smod files, directories are searched in this order:

1. In the directory of the source file that contains the USE statement

2. In the directories specified by compiler option module path

3. In the current working directory

4. In the directories specified by compiler options -I dir (Linux* and macOS) or /include (Windows*)

5. In the directories specified with environment variables CPATH or INCLUDE

6. In the standard system directories

If you are building your program as part of a Microsoft Visual Studio* project that has dependent projects, the Fortran build system automatically searches the dependent projects for compiled .mod files.

### Note

You must make sure that the module files are created before they are referenced by another program or subprogram. If the module and its USE statement are in the same source file, the module must appear first.

You can specify compiler option gen-dep to create a list of compiled module files that a source file depends upon. This list can be used with build utilities such as `make` to specify dependencies and to ensure that files that use a module are recompiled when the module is modified and recompiled.

## Compiling Programs with Modules

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

For example, consider that 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

When the following command is specified:

    ifort -ca.f90

It generates the following files:

- test.mod

- payroll.mod

- a.o (Linux* and macOS)

- a.obj (Windows*)

The .mod files contain the necessary information about 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:

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

When you compile the program as follows:

    ifort -c mod_def.f90

It produces the object files mod_def.o (Linux* and macOS) or mod_def.obj (Windows*) and also the .mod file definedmod.mod - all in the current directory.

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

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

Example

Consider that the current directory contains the files mod_def.f90 and use_mod_def.f90, and it also contains the subdirectory my_modules.

If you execute the following command on Linux* or macOS, it creates the file definedmod.mod in the subdirectory my_modules, and it creates the file mod_def.o in the current directory:

    ifort -c -module ./my_modules mod_def.f90

If you then execute either of the following commands, it creates the executable file ./a.out:

    ifort -c -module ./my_modules use_mod_def.f90 mod_def.o

    ifort -c -I ./my_modules use_mod_def.f90 mod_def.o

Parent topic: Program Structure

## See Also

gen-dep compiler option
I compiler option
module compiler option
SUBMODULE statement
USE statement