Include directive

Uses your system voice; voice and rate in settings.

Snapshot

≈ 8 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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 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 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.…

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 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 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 namespaces. 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. // 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 'MyHeader.h' include "MyHeader.h" The include directive allows for the development of code libraries that: helps 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 central/general change programs when needed (only one file should be edited) save time by reusing data layouts Include directives do not support globbing patterns, so for example #include "*.h" would not include all files in the current directory. pragma once 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: include "Add.h" int triple(int x) { return add(x, add(x, x)); } C standard header files are named with a file name extension, as in #include . Typically, custom C header files have the same extension. Custom C++ header files tend to have more variety of extensions, including , , and as well as the ambiguous . A C++ standard library name in angle brackets (i.e. ) results in declarations being included but may not be from a file. import ; // supporting this is optional import ; // supporting this is mandated by the standard export import "MyHeader.h"; Header units are provided for all the C++ standard library headers. As of C++23, the C++ standard library can be imported as a module by writing import std; . // imports the C++ standard library import std; // 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; constexpr char ICON_DISPLAY_DATA[] = { #embed "art.png" }; // specify any type which can be initialized form integer constant expressions will do constexpr char RESET_BLOB[] = { #embed "data.bin" }; // attributes work just as well alignas(8) constexpr char ALIGNED_DATA_STRING[] = { #embed "attributes.xml" }; int main() { return embed limit(1) ; } import import "MyClass.h" 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. The Haskell language, which can use the C preprocessor by writing {-# LANGUAGE CPP #-} , has access to the #include directive. {$i filename.pas} (*$i filename.inc*) {$include filename.inc} (*include filename.pas*) Where the $i / $I or $include / $INCLUDE directive is not case sensitive, and or is the name of the file to be included. (It has been common practice to name Pascal's include files with the extension , 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. include!("generated_code.rs"); 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. let my_str: &'static str = include_str!("spanish.in"); let bytes: &'static [u8] = include_bytes!("spanish.in"); include ... (Fortran, MASM) (HTML SSI) var ... = require("..."); (JavaScript with CommonJS) (JSP) %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 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). ==See also==

Text derived from https://en.wikipedia.org/w/index.php?oldid=1362297417 under CC BY-SA 4.0; modifications have been made.