An include directive instructs a Text processing 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 file.
The include directive can support encapsulation and code 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 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 Go, Python and Haskell support modularization via an import statement, which makes the compiler/interpreter load a module, resolving code through the linked module, not by including text. Compiled languages, such as Rust and D, simply link all object files at compile time. Similarly, C++ also introduced import to import C++ modules, which uses files to store as intermediates similar to how precompiled headers work. Although Java has import and C# has using, these are not the same as an include directive. In fact, in these languages, classes are loaded on demand by a class loader, and can be accessed simply by fully qualifying the class with its . The import statements in these languages are used only to add a class to the current scope.
Although C# has the ability to use some preprocessor directives similar to those of the C preprocessor, it does not contain the #include directive.
Example include statements:
// include the C++ standard header 'vector'; may or may not be a file
// include a custom header file named 'MyHeader.h'
The include directive allows for the development of code libraries that:
Include directives do not support globbing patterns, so for example
int add(int, int);
int triple(int x) {
return add(x, add(x, 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 , the using source file becomes:
int triple(int x) {
return add(x, add(x, x));
}
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 file name extension, as in
A C++ standard library name in angle brackets (i.e. ) results in declarations being included but may not be from a file.C11 standard, 7.1.2 Standard headers, p. 181, footnote 182: "A header is not necessarily a source file, nor are the < and > delimited sequences in header names necessarily valid source file names.
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:
Header units are provided for all the C++ standard library headers.
exportoptional import module_name;
As of C++23, the C++ standard library can be imported as a module by writing
// imports a module named "acme.project.math.BigInteger"
import acme.project.math.BigInteger;
// imports a module named "wikipedia.util.logging.Logger", and re-exports it
// any file that imports this module will transitively import wikipedia.util.logging.Logger
export import wikipedia.util.logging.Logger;
// specify any type which can be initialized form integer constant expressions will do
const char resetBlob = {
// attributes work just as well
const signed char alignedDataString __attribute__ ((aligned (8))) = {
int main() {
#embed "art.png"
};
#embed "data.bin"
};
#embed "attributes.xml"
};
return
;
}
The Objective-C #import directive should not be confused for the import statement in C++ (which imports a module), or the Microsoft Visual C++ #import directive, which imports a type library.
fn main() {
// ... use items from generated_code.rs
}
However, unlike C, this is not the usual method of separating and including code. Instead, Rust uses modules, which preserve namespaces and encapsulation.
Rust also supports an include_str! macro which instead allows an entire file to be included and stored into a string. There is similarly a include_bytes! macro.
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).
|
|