Jump to content

Include directive

From Wikipedia, the free encyclopedia
(Redirected from Header files)

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 .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 implementation. The included information can be minimized to only declarations.

Header file

[edit]

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 implementation.[1]

The C standard library is declared as a collection of header files. The C++ standard library can be considered to be similar, but in practice the declarations may be provided by the compiler without providing a header file.

As many consider that including has significant drawbacks, newer languages have been designed without an include directive or header files. Languages such as Java and C# support modularization via a more logical import concept.

A header file and the include directive allow libraries of code to be developed which:

  • 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

[edit]

Given two C source files. One defines a function add() and another uses the function. Without using a header file, the consuming file can declare the function locally as a function prototype:

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 add.h, the using source file becomes:

#include "add.h"
int triple(int x) {
    return add(x, add(x,x));
}

Language support

[edit]

C/C++

[edit]

In the C and C++ programming languages, the #include preprocessor directive causes the compiler to replace that line with the entire text of the contents of the named source file (if included in quotes: "") or named header (if included in angle brackets: <>);[2] a header doesn't need to be a source file.[3] 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" (sometimes ".hpp" or ".hh" to distinguish C++ headers), though files with .c, .cc, and .cpp extensions may also be included (particularly in the single compilation unit technique), 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 (or set of directories), and then searches for source files in local or project-specific paths (specified on the command line, in an environment variable, or in a Makefile or other build file), while the form with quotes does not search in a standard system directory, only searching in local or project-specific paths.[4] 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 is used to omit the .h extension in including C++ standard headers; in common use, "header" means "header file".

For example:

#include <stdio.h>  // Include the contents of the standard header 'stdio.h' (probably a file 'stdio.h').
#include <vector>  // Include the contents of the standard header 'vector' (probably a file 'vector.h').
#include "user_defined.h"  // Include the contents of the file 'user_defined.h'.

In C and C++, problems may be faced if two (or more) include files contain 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.[5]

Since C++20, headers can also be imported as header units, that is, separate translation units synthesized from a header.[6] They are meant to be used alongside modules. The syntax used in that case is

exportoptional import header-name;

For example:

import <stdio.h>; // supporting this is optional
import <vector>; // supporting this is mandated by the standard
export import "user_defined.h";

Header units are provided for all the C++ standard library headers.[7]

COBOL

[edit]

COBOL (and also RPG IV) allows programmers to copy copybooks into the source of the program in a similar way to header files, but it also allows for the replacement of 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[8] to COPY by 1968.[9]

Fortran

[edit]

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.

Pascal

[edit]

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.pas}
  • (*$I filename.inc*)
  • {$include 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

[edit]

In PHP, the include directive causes another PHP file to be included and evaluated.[10] Similar commands are require, which upon failure to include will produce a fatal exception and halt the script,[11] 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

[edit]

There are many forms of the include directive, such as:

  • include ... (Fortran, MASM)
  • <!--#include ... --> (HTML SSI)
  • import ...; (Java)
  • import ... from ... (JavaScript as in ECMAScript)
  • var ... = require("...") (JavaScript with CommonJS)
  • <%@ include ... %> (JSP)
  • {$I ...} (UCSD Pascal, Turbo Pascal)
  • %include ... (PL/I)
  • import ... (Python)
  • /COPY QCPYLESRC,QBC (RPG IV – first argument is the filename, second argument is the copybook)
  • use ...; (Rust)
  • using ...; (C#)
  • local ... = require("...") (Lua)
  • import ...; (D)

Modern languages (e.g. Haskell and Java) tend to avoid copybooks or includes, preferring modules and import/export systems for namespace control. 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), meaning header files are not needed.

See also

[edit]

References

[edit]
  1. ^ Alan Griffiths (2005). "Separating Interface and Implementation in C++". ACCU. Retrieved 2013-05-07.
  2. ^ C11 standard, 6.10.2 Source file inclusion, pp. 164–165
  3. ^ 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.
  4. ^ Stallman, Richard M. (July 1992). "The C Preprocessor" (PDF). Archived from the original (PDF) on 4 September 2012. Retrieved 19 February 2014.
  5. ^ Pike, Rob (21 Feb 1989), Notes on programming in C, Cat-v document archive, retrieved 9 Dec 2011
  6. ^ "Merging Modules - P1103R3" (PDF).
  7. ^ "P1502R1 - Standard library header units for C++20".
  8. ^ "COBOL Initial Specifications for a COmmon Business Oriented Language" (PDF). Department of Defense. April 1960. p. IX-9. Archived from the original (PDF) on 12 February 2014. Retrieved 11 February 2014.
  9. ^ "The COPY Statement". CODASYL COBOL Journal of Development 1968. July 1969. LCCN 73601243.
  10. ^ "include". php.net. The PHP Group. Retrieved 20 February 2014.
  11. ^ "require". php.net. The PHP Group. Retrieved 20 February 2014.
[edit]