9.1 File Inclusion

A file can be included as follows:

  1. -include(File).
  2. -include_lib(File).

File, a string, is to point out a file. The contents of this file are included as is, at the position of the directive.

Include files are typically used for record and macro definitions that are shared by several modules. It is recommended to use the file name extension .hrl for include files.

File can start with a path component $VAR, for some string VAR. If that is the case, the value of the environment variable VAR as returned by os:getenv(VAR) is substituted for $VAR. If os:getenv(VAR) returns false, $VAR is left as is.

If the filename File is absolute (possibly after variable substitution), the include file with that name is included. Otherwise, the specified file is searched forin the following directories, and in this order:

  • The current working directory
  • The directory where the module is being compiled
  • The directories given by the include optionFor details, see the erlc(1) manual page in ERTS and compile(3) manual page in Compiler.

Examples:

  1. -include("my_records.hrl").
  2. -include("incdir/my_records.hrl").
  3. -include("/home/user/proj/my_records.hrl").
  4. -include("$PROJ_ROOT/my_records.hrl").

include_lib is similar to include, but is not to point out an absolute file. Instead, the first path component (possibly after variable substitution) is assumed to be the name of an application.

Example:

  1. -include_lib("kernel/include/file.hrl").

The code server uses code:lib_dir(kernel) to find the directory of the current (latest) version of Kernel, and then the subdirectory include is searched for the file file.hrl.