# Include directive

An include directive instructs a text file processor to replace the directive text with the content of a specified file.
The act of including may be logical in nature. The processor may simply process the include file content at the location of the directive without creating a combined file.
Different processors may use different syntax. The C preprocessor (used with C, C++ and in other contexts) defines an include directive as a line that starts #include and is followed by a file specification. COBOL defines an include directive indicated by copy in order to include a copybook.
Generally, for C/C++ the include directive is used to include a header file, but can include any file. Although relatively uncommon, it is sometimes used to include a body file such as a .c file.
The include directive can support encapsulation and reuse. Different parts of a system can be segregated into logical groupings yet rely on one another via file inclusion. C and C++ are designed to leverage include while also optimizing build time by allowing declaration separate from implementation. The included information can be minimized to only declarations.
As many consider that including file content has significant drawbacks, newer languages have been designed without an include directive. Languages such as Java and C# support modularization via an import concept that allows a module to use the assets of another module at a conceptual level; not by including text.

## Language support
### C/C++

Both C and C++ are typically used with the C preprocessor that replaces a #include directive line with the content of the file specified. A file path is either enclosed in double quotes (i.e. "xyz.h") or angle brackets (i.e. <xyz.h>). Some preprocessors locate the include file differently based on the enclosing delimiters; treating a path in double-quotes as relative to the including file and a path in angle brackets as located in one of the directories of the configured system search path.
Example include statements:
The include directive allows for the development of code libraries that:
* ensure that everyone uses the same version of a data layout definition or procedural code throughout a program
* easily cross-reference where components are used in a system
* easily change programs when needed (only one file must be edited)
* save time by reusing data layouts

#### Example

Given two C source files. One defines a function add and another uses the function. Without using an include directive, the consuming file can declare the function locally as a function prototype:
One drawback of this approach is that the function prototype must be present in each file that calls add. Another drawback is that if the signature changes, then each consuming file needs to be updated. Putting the prototype in a single, separate file avoids these issues. If the prototype is moved to a header file add.h, the using source file becomes:

#### Header file

In C and C++, a header file is a source code file that allows programmers to separate elements of a codebase – often into reusable, logically-related groupings.
A header file declares programming elements such as functions, classes, variables, and preprocessor macros. A header file allows the programmer to use programming elements in multiple body files based on the common declaration in the header file. Declarations in a header file allow body files to use implementations without including the implementation code directly. The header keeps the interface separate from the implementation.
Compilation errors may occur if multiple header files include the same file. One solution is to avoid including files in header files – possibly requiring excessive include directives in body files. Another solution is to use an include guard in each header file.
The C standard library is declared as a collection of header files. The C++ standard library is similar, but the declarations may be provided by the compiler without reading an actual file.
C standard header files are named with a .h file name extension, as in #include <stdio.h>. Typically, custom C header files have the same extension. Custom C++ header files tend to have more variety of extensions, including .hpp, .h++ and .hh.

#### Header

A C++ standard library name in angle brackets (i.e. <vector>) results in declarations being included but may not be from a file.

#### Header unit

Since C++20, C++ supports import semantics via the header unit, that is, separate translation units synthesized from a header. They are meant to be used alongside modules. The syntax used in that case is:
Example:
Header units are provided for all the C++ standard library headers.

### COBOL

COBOL (and also RPG IV) allows programmers to copy copybooks into the source of the program – which is similar to including but allows for replacing text. The COBOL keyword for inclusion is COPY, and replacement is done using the REPLACING ... BY ... clause. An include directive has been present in COBOL since COBOL 60, but changed from the original INCLUDE to COPY by 1968.

### Fortran

Fortran does not require header files per se. However, Fortran 90 and later have two related features: include statements and modules. The former can be used to share a common file containing procedure interfaces, much like a C header, although the specification of an interface is not required for all varieties of Fortran procedures. This approach is not commonly used; instead, procedures are generally grouped into modules that can then be referenced with a use statement within other regions of code. For modules, header-type interface information is automatically generated by the compiler and typically put into separate module files, although some compilers have placed this information directly into object files. The language specification itself does not mandate the creation of any extra files, even though module procedure interfaces are almost universally propagated in this manner.

### Pascal

Most Pascal compilers support the $i or $include compiler directive, in which the $i or $include directive immediately follows the start of a comment block in the form of
* {$i filename.pas}
* (*$I filename.inc*)
* {$include filename.inc}
* (*INCLUDE filename.pas*)
Where the $i or $include directive is not case sensitive, and filename.pas or filename.inc is the name of the file to be included. (It has been common practice to name Pascal's include files with the extension .inc, but this is not required.) Some compilers, to prevent unlimited recursion, limit invoking an include file to a certain number, prohibit invoking itself or any currently open file, or are limited to a maximum of one include file at a time, e.g. an include file cannot include itself or another file. However, the program that includes other files can include several, just one at a time.

### PHP

In PHP, the include directive causes another PHP file to be included and evaluated. Similar commands are require, which upon failure to include will produce a fatal exception and halt the script, and include_once and require_once, which prevent a file from being included or required again if it has already been included or required, avoiding the C's double inclusion problem.

### Other languages

Other notable languages with an include directive:
* include ... (Fortran, MASM)
* <!--#include ... --> (HTML SSI)
* var ... = require("...") (JavaScript with CommonJS)
* <%@ include ... %> (JSP)
* {$I ...} (UCSD Pascal, Turbo Pascal)
* %include ... (PL/I)
* /COPY QCPYLESRC,QBC (RPG IV – first argument is the filename, second argument is the copybook)
* local ... = require("...") (Lua)
* import ...; (D)
Modern languages (e.g. Haskell and Java) tend to avoid the include directive construct, preferring modules and import/export semantics. Some of these languages (such as Java and C#) do not use forward declarations and, instead, identifiers are recognized automatically from source files and read directly from dynamic library symbols (typically referenced with import or using directives).