Clicky

Fortran Wiki
Include files

INCLUDE files

Fortran include files use the INCLUDE statement to insert the contents of a separate source file directly into a program during compilation. This mechanism allows programmers to share common blocks of code, such as constant definitions or common block declarations, across multiple parts of a program or multiple source files, ensuring consistency.

It also may be used to provide a very basic templating method, allowing code to be reused multiple times for different types or kinds.

Key Characteristics

Function: The compiler effectively replaces the INCLUDE line with
          the complete text of the specified file before processing
          the code.

Content: Included files must contain only valid Fortran
         statements. They cannot begin or end with an incomplete
         statement or a continuation line.

Syntax: The general syntax is INCLUDE 'filename' or INCLUDE 'filename.ext'.
        The filename can be a character literal constant.
        Some compilers support optional parameters for conditional
        inclusion or source listing controls (e.g., /[NO]LIST).

    An **INCLUDE** statement _must not be continued__.

    The compiler is free to support other rules that may even permit
    URLs to be included in order to access remote files; but this is
    rare and very non-portable. Check your compiler documentation to
    see if any such syntax is supported.

File Search Path: The compiler typically searches for include files
                  in a specific order:

    + The directory containing the source file with the INCLUDE statement.
    + The current working directory.
    + Directories specified by command-line options (e.g., -I on Linux/macOS, /include on Windows).
    + Directories specified by environment variables (e.g., FPATH or INCLUDE).
    + Standard system directories.

Nesting: Include files can contain other INCLUDE statements, but
         they cannot include themselves (recursive inclusion is
         not allowed).

Fortran Modules vs. Include Files

While include files were essential in older Fortran versions (like Fortran 77), modern Fortran (Fortran 90 and later) introduced modules, which are generally preferred.

INCLUDE versus cpp #include statement

A file read with INCLUDE is likely to not expand cpp(1) directives. You probably want to change your INCLUDE to a “#include” to avoid the issue.

The search path used to locate the files may be different for INCLUDE versus cpp(1) “#include” statements. See your compiler documentation.

Templating

Preprocessors are usually used to support templating in Fortran. But INCLUDE can be used to help generate generic procedures. A trivial example given a source file

module M_swap
private
public :: swap

interface swap
   module procedure swap_int8
   module procedure swap_real64
end interface

contains

elemental subroutine swap_real64(x,y)
type(real(kind=real64)), intent(inout) :: x,y
type(real(kind=real64))                :: temp
   INCLUDE "swapscalar.inc"
end subroutine swap_real64

elemental subroutine swap_int8(x,y)
type(integer(kind=int8)), intent(inout) :: x,y
type(integer(kind=int8))                :: temp
   INCLUDE "swapscalar.inc"
end subroutine swap_int8
end module M_swap

and an include file

swapscalar.inc

   temp = x; x = y; y = temp

the code in “swapscalar.inc” can be reused for different kinds or types. Obviously, a more substantive include file than that shown, comprised of much more extensive code would warrant using INCLUDE this way.