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
The C preprocessor (CPP) is a text file processor that is used with C, C++ and other programming tools. The preprocessor provides for file inclusion (often header files), macro expansion, conditional compilation, and line control. Although ...
(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
COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural, and, since 2002, object-oriented language. COBOL is primarily ...
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
Reuse is the action or practice of using an item, whether for its original purpose (conventional reuse) or to fulfill a different function (creative reuse or repurposing). It should be distinguished from recycling, which is the breaking down of ...
. 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
Implementation is the realization of an application, execution of a plan, idea, scientific modelling, model, design, specification, Standardization, standard, algorithm, policy, or the Management, administration or management of a process or Goal ...
. The included information can be minimized to only declarations.
As directly including file content has significant drawbacks, such as excessive boilerplate or type/language syntax unawareness, newer languages have been designed without an include directive. Languages such as
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
and
C# support
modularization via an import concept that allows a package or module to use the assets of another module at a conceptual level by loading the associated
Java class file
A Java class file is a file (with the filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is usually produced by a Java compiler from Java programming language source files ...
or C#
DLL as necessary; not by including text. Compiled languages, such as
Rust
Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
and
D, simply link all
object files at compile time, and C++ also introduced
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:
// include the C standard header 'stdio.h'; probably is a file with that name
#include
// include the C++ standard header 'vector'; may or may not be a file
#include
// include a custom header file named 'user_defined.h'
#include "user_defined.h"
The include directive allows for the development of code
libraries
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
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
In computer programming, a function prototype is a declaration of a function that specifies the function's name and type signature (arity, data types of parameters, and return type), but omits the function body. While a function definition ...
:
int add(int, int);
int triple(int x)
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:
#include "add.h"
int triple(int x)
Header file
In C and C++, a header file is a
source code
In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer.
Since a computer, at base, only ...
file that allows
programmer
A programmer, computer programmer or coder is an author of computer source code someone with skill in computer programming.
The professional titles Software development, ''software developer'' and Software engineering, ''software engineer' ...
s to separate elements of a
codebase
In software development, a codebase (or code base) is a collection of source code used to build a particular software system, application, or software component. Typically, a codebase includes only human-written source code system files; thu ...
often into reusable, logically-related groupings.
A header file
declares programming elements such as
functions,
class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
es,
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
Implementation is the realization of an application, execution of a plan, idea, scientific modelling, model, design, specification, Standardization, standard, algorithm, policy, or the Management, administration or management of a process or Goal ...
.
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 the C and C++ programming languages, an #include guard, sometimes called a macro guard, header guard or file guard, is a way to avoid the problem of ''double inclusion'' when dealing with the include directive.
The C preprocessor processe ...
in each header file.
The
C standard library
The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
is declared as a collection of header files. The
C++ standard library
The C standard library, sometimes referred to as libc, is the standard library for the C programming language, as specified in the ISO C standard.ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the origina ...
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
A filename extension, file name extension or file extension is a suffix to the name of a computer file (for example, .txt, .mp3, .exe) that indicates a characteristic of the file contents or its intended use. A filename extension is typically d ...
, as in
#include . 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
.
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 C20 or C-20 may refer to:
Science and technology
* Carbon-20 (C-20 or 20C), an isotope of carbon
* C20, the smallest possible fullerene (a carbon molecule)
* C20 (engineering), a mix of concrete that has a compressive strength of 20 newtons per squ ...
, 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 C26 or C-26 may refer to:
* C.26, a British World War I Coastal class airship
* C26 road (Namibia)
* Caldwell 26, a spiral galaxy
* Caspar C 26, German sport plane
* Douglas C-26 Dolphin, an American military flying boat
* Fairchild C-26 Metro ...
, 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:
import ; // supporting this is optional
import ; // supporting this is mandated by the standard
export import "user_defined.h";
Header units are provided for all the C++ standard library headers.
Modules
Since C++20,
modules were introduced and is imported using
import
. 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:
export
optional import module_name;
As of
C++23
C++23, formally ISO/IEC 14882:2024, is the current open standard for the C++ programming language that follows C++20. The final draft of this version is N4950.
In February 2020, at the final meeting for C++20 in Prague, an overall plan for C++ ...
, the
C++ standard library
The C standard library, sometimes referred to as libc, is the standard library for the C programming language, as specified in the ISO C standard.ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the origina ...
can be imported as a module by writing
import std;.
Embed
Added in
C23 and
C++26 C26 or C-26 may refer to:
* C.26, a British World War I Coastal class airship
* C26 road (Namibia)
* Caldwell 26, a spiral galaxy
* Caspar C 26, German sport plane
* Douglas C-26 Dolphin, an American military flying boat
* Fairchild C-26 Metro ...
, 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
Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was ...
, 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
COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural, and, since 2002, object-oriented language. COBOL is primarily ...
(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.
Haskell
The
Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
language, which can use the C preprocessor by writing
, has access to the
#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
PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. ...
, 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)
*
(
UCSD Pascal
UCSD Pascal is a Pascal programming language system that runs on the UCSD p-System, a portable, highly machine-independent operating system. UCSD Pascal was first released in 1977. It was developed at the University of California, San Diego (UC ...
,
Turbo Pascal
Turbo Pascal is a software development system that includes a compiler and an integrated development environment (IDE) for the programming language Pascal (programming language), Pascal running on the operating systems CP/M, CP/M-86, and MS-DOS. ...
)
*
%include ...
(
PL/I
PL/I (Programming Language One, pronounced and sometimes written PL/1) is a procedural, imperative computer programming language initially developed by IBM. It is designed for scientific, engineering, business and system programming. It has b ...
)
*
/COPY ''QCPYLESRC'',''QBC''
(RPG IV – first argument is the filename, second argument is the copybook)
*
local ... = require("...")
(
Lua)
Modern languages (e.g.
Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
and
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
) 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
A dynamic library is a library that contains functions and data that can be consumed by a computer program at run-time as loaded from a file separate from the program executable. Dynamic linking or late binding allows for using a dynamic libra ...
symbols (typically referenced with
import
or
using
directives).
See also
*
*
*
*
*
*
*
*
*
*
*
References
External links
Organizing Code Files (the potential pitfalls and guidelines for using header files in C++)
{{DEFAULTSORT:Include Directive
Programming constructs
Source code
C (programming language)
C++
fr:Bibliothèque standard du C#Les en-têtes de la bibliothèque C ISO