Header file

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In computer programming, particularly in the C and C++ programming languages, a header file or include file is a file, usually in the form of source code, that a compiler automatically includes when processing another source file. Typically, programmers specify the inclusion of header files via compiler directives at the beginning (or head) of the other source file.

A header file commonly contains forward declarations of classes, subroutines, variables, and other identifiers. Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required.

The C standard library and C++ standard library traditionally declare their standard functions in header files.

Contents

[edit] Motivation

In most modern computer programming languages, programmers can break up programs into smaller components (such as classes and subroutines) and distribute those components among many translation units (typically in the form of physical source files), which the system can compile separately. Once a subroutine needs to be used somewhere other than in the translation unit where it is defined, a way to refer to it must exist. For example, a function defined in this way in one source file:

int add(int a, int b) {
	return a + b;
}

may be 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));
}

The drawbacks of this method are obvious: the prototype must be present in all files that use the function and, should the function change its return type or arguments, these prototypes will have to be updated. This process can be automated with the C preprocessor, so that in getting any prototype results in getting the latest one. For example, the following code declares the function in a separate file:

#ifndef ADD_H_GUARD
#define ADD_H_GUARD
int add(int,int);
#endif

This file uses include guards to avoid illegal multiple definitions of the function. The following code demonstrates how header files are used:

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

[edit] Alternatives

Some newer languages (such as Java) dispense with header files and instead use a naming scheme that allows the compiler to locate the source files associated with interfaces and class implementations (but in doing so, restrict file-naming freedom). These languages(and perhaps others) preserve type information for functions in the object code, allowing the linker to verify that functions are used correctly.

COBOL and RPG IV have a form of include files called copybooks. Programmers "include" these into the source of the program in a similar way to header files, but they also allow replacing certain text in them with other text. The COBOL keyword for inclusion is COPY, and replacement is done with a REPLACING ... BY ... clause.

[edit] See also

[edit] External links

Personal tools