#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 import
to import modules.
Although C# has the ability to use some preprocessor directives similar to those of the C preprocessor, it does not contain the #include
directive.
Language support
C/C++
Both C and C++ (pre-C++20) 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. ) or angle brackets (i.e. ). 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:
Example
Given two C source files. One defines a functionadd()
and another uses the function. Without using an include directive, the consuming file can declare the function locally as a function prototype:
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.h
file name extension, as in .hpp
, .h++
and .hh
.
A C++ standard library name in angle brackets (i.e. ) 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 as a transitional state towards total adoption of modules.import
, as of C++26, is not a preprocessor directive. It is thus not handled by the C preprocessor. import
does not copy code into a file like #include
, but rather links the translation unit during compile time.
Example:
Modules
Since C++20, modules were introduced and is imported usingimport
. These have the same semantics to import as header units, but have finer grained control of exports. They must be marked module
and each symbol must be marked export
or be in a export
block to be accessible by importing. The semantics of an import
statement are:
exportoptional import module_name;
As of C++23, the C++ standard library can be imported as a module by writing Embed
Added in C23 and C++26, the#embed
directive is similar to the #include
directive but is more appropriate for including/embedding binary resources into source code.
Objective-C
Objective-C, like C, also uses header files and has an#include
directive. However, it also has a #import
directive, which behaves the same as #include
, the only difference being that #import
guarantees the inclusion of the file will never happen more than once, without the use of guards or .
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 isCOPY
, 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.
Haskell
The Haskell language, which can use the C preprocessor by writing#include
directive.
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.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, theinclude
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)
*
( 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)
Modern languages (e.g. Haskell and import
or using
directives).
See also
* * * * * * * * * * *References
External links