Many programming languages and other computer files have a directive, often called include, that causes the contents of a second file to be inserted into the original file. These included files are called copybooks or s. They are often used to define the physical layout of program data, pieces of procedural code and/or forward declarations while promoting encapsulation and the reuse of code.
Purpose
The include directive allows libraries of code to be developed which help to:
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.
save time by not needing to code extensive data layouts.
An example situation which benefits from the use of an include directive is when referring to functions in a different file. Suppose we have a function add in one file, which is then declared and then referred to in a second source file as follows: int add; int triple
One drawback of this method is that the prototype must be present in all files that use the function. Another drawback is that if the return type or arguments of the function are changed, these prototypes will have to be updated. Putting the prototype in a single, separate file avoids these problems. Assuming the prototype is moved to the file add.h, the second source file can then become:
include "add.h"
int triple
Now, every time the code is compiled, the latest function prototypes in add.h will be included in the files using them, avoiding potentially disastrous errors.
Language support
C/C++
In C and C++, the #includepreprocessor directive causes the compiler to replace that line with the entire text of the contents of the named source file or named header ; note that a header need not be a source file. Inclusion continues recursively on these included contents, up to an implementation-defined nesting limit. Headers need not have names corresponding to files: in C++ standard headers are typically identified with words, like "vector", hence #include <vector> while in C standard headers have identifiers in the form of filenames with a ".h" extension, as in #include <stdio.h>. A "source file" can be any file, with a name of any form, but is most commonly named with a ".h" extension and called a "header file", though files with.c,.cc, and.cpp extensions may also be included, and sometimes other extensions are used. These two forms of #include directive can determine which header or source file to include in an implementation-defined way. In practice, what is usually done is that the angle-brackets form searches for source files in a standard system directory, and then searches for source files in local or project-specific paths, while the form with quotes does not search in a standard system directory, only searching in local or project-specific paths. In case there is no clash, the angle-brackets form can also be used to specify project-specific includes, but this is considered poor form. The fact that headers need not correspond to files is primarily an implementation technicality, and used to omit the.h extension in including C++ standard headers; in common use "header" means "header file". For example:
include // Include the contents of the standard header 'stdio.h'.
include // Include the contents of the standard header 'vector'.
include "user_defined.h" // Include the contents of the file 'user_defined.h'.
In C and C++, problems may be faced if two include files both in turn include the same third file. One solution is to avoid include files from including any other files, possibly requiring the programmer to manually add extra include directives to the original file. Another solution is to use include guards.
COBOL
allows programmers to copy copybooks into the source of the program in a similar way to header files, but it also allows replacing certain text in them with other 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.
does not require header files per se. However, Fortran 90 and later has 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.
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 cause a file to not be included or required again if it has already been included or required, avoiding the C's double inclusion problem.
Other languages
There are many forms of the include directive, such as:
#include...
include...
<!--#include... -->
import...;
import... from...
var... = require
<%@ include... %>
%include...
import...
/COPY QCPYLESRC,QBC
extern crate...;
Modern languages tend to avoid copybooks or includes, preferring modules and import/export systems for namespace control. Some of these languages do not use forward declarations and, instead, identifiers are recognized automatically from source files and read directly from dynamic library symbols, meaning header files are not needed.