Include directive

From Wikipedia, the free encyclopedia
  (Redirected from Header files)
Jump to: navigation, search

Many programming languages and other computer files have a directive, often called include (as well as copy and import), that causes the contents of a second file to be inserted into the original file. These included files are called copybooks or header files. They are often used to define the physical layout of program data, pieces of procedural code and/or forward declarations while promoting encapsulation and the reuse of code.

Purpose[edit]

The include directive allows libraries of code to be developed which help to:

  • 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 master copy to change).
  • save time by not needing to code extensive data layouts (minor, but useful).

An example situation which benefits from the use of an include directive is when referring to functions in a different file. Suppose we have a function add in one file, which is then declared (with a function prototype) and then referred to in a second source file as follows:

int add(int, int);
 
int triple(int x)
{
    return add(x, add(x, x));
}

One drawback of this method is that the prototype must be present in all files that use the function. Another drawback is that if the return type or arguments of the function are changed, these prototypes will have to be updated. Putting the prototype in a single, separate file avoids these problems. Assuming the prototype is moved to the file add.h, the second source file can then become:

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

Now, every time the code is compiled, the latest function prototypes in add.h will be included in the files using them, avoiding potentially disastrous errors.

Language Support[edit]

C[edit]

In C, the #include preprocessor directive causes the compiler to replace that line with the entire text of the contents of the header file. There are two main variants of #include in C that uses two different file search strategies. The first variant has the header filepath within a pair of angle brackets, indicating that the file is located using a search strategy that prefers standard headers to other headers having the same name. The other variant includes the filepath within two double quotes and is used to include local or project-specific header files.[1]

For example:

#include <stdio.h>  // This will make the compiler to include the contents of standard header file 'stdio.h'.
#include "user_defined.h" // This will make the compiler to include the contents of 'user_defined.h' which is not a standard header.

In C, problems may be faced if two (or more) include files both in turn include 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.[2]

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 replacing 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[3] to COPY by 1968.[4]

Fortran[edit]

Fortran does not require header files per se. However, Fortran 90 and later has 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.

PHP[edit]

In PHP, the include directive causes another PHP file to be included and evaluated.[5] Similar commands are require, which upon failure to include will produce a fatal error and halt the script,[6] and include_once and require_once, which cause a file to not be 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:

  • /COPY QCPYLESRC,QBC (RPG IV – first argument is the filename, second argument is the copybook)
  • include ... (Fortran, MASM)
  • %include ... (PL/I)
  • <!--#include ... --> (HTML SSI)
  • <%@ include ... %> (JSP).

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, meaning header files are not needed.

See also[edit]

References[edit]

  1. ^ Stallman, Richard M. (July 1992). "The C Preprocessor" (PDF). Archived from the original on 4 September 2012. Retrieved 19 February 2014. 
  2. ^ Pike, Rob (21 Feb 1989), "Notes on programming in C", Cat-v document archive, retrieved 9 Dec 2011 
  3. ^ "COBOL Initial Specifications for a COmmon Business Oriented Language". Department of Defense. April 1960. p. IX-9. Archived from the original on 12 February 2014. Retrieved 11 February 2014. 
  4. ^ "The COPY Statement". CODASYL COBOL Journal of Development 1968. July 1969. LCCN 73601243. 
  5. ^ "include". php.net. The PHP Group. Retrieved 20 February 2014. 
  6. ^ "require". php.net. The PHP Group. Retrieved 20 February 2014. 

External links[edit]