Header file
Encyclopedia
Some programming languages (most notably C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

, and Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

) use header files. These files allow programmers to separate certain elements of a program's source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 into reusable files. Header files commonly contain forward declarations of class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

es, subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

s, variables, and other identifier
Identifier
An identifier is a name that identifies either a unique object or a unique class of objects, where the "object" or class may be an idea, physical [countable] object , or physical [noncountable] substance...

s. 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. This is to keep the interface in the header separate from the implementation. The C standard library
C standard library
The C Standard Library is the standard library for the programming language C, as specified in the ANSI C standard.. It was developed at the same time as the C POSIX library, which is basically a superset of it...

 and C++ standard library
C++ standard library
In C++, the C++ Standard Library is a collection of classes and functions, which are written in the core language and part of the C++ ISO Standard itself...

 traditionally declare their standard functions in header files.

Newer compiled languages (such as Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

, C#) do not use forward declarations; identifiers are recognized automatically from source files and read directly from dynamic library symbols. This means header files are not needed.

Motivation

In most modern computer programming languages, programmers can break up programs
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 into smaller components (such as class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

es and subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

s) and distribute those components among many translation units
Translation unit (programming)
In C programming language terminology, a translation unit is the ultimate input to a C compiler from which an object file gets generated.-Context:...

 (typically in the form of physical source file
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...

s), which the system can compile
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 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
Function prototype
A function prototype in C, Perl or C++ is a declaration of a function that omits the function body but does specify the function's return type, name, arity and argument types...

) 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
Function prototype
A function prototype in C, Perl or C++ is a declaration of a function that omits the function body but does specify the function's return type, name, arity and argument types...

 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. This process can be automated with the C preprocessor
C preprocessor
The C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....

 (i.e., getting any prototype results in getting the latest one). For example, the following code declares the function in a separate file (the parameter
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...

 names are not used by the compiler, but they can be useful to programmers):

File "add.h"
  1. ifndef ADD_H_GUARD
  2. define ADD_H_GUARD

int add(int a, int b);
  1. endif



This file uses include guard
Include guard
In the C and C++ programming languages, an #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive...

s to avoid multiple definitions of the function. The following code demonstrates how header files are used:

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

Alternatives

Some newer languages (such as Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

) dispense with header files and instead use a naming scheme
Naming scheme
A naming scheme is a plan for naming objects. In computing, naming schemes are often used for objects connected into computer networks.-Naming schemes in computing:Large networks often use a systematic naming scheme, such as using a location A naming scheme is a plan for naming objects. In...

 that allows the compiler to locate the source files associated with interfaces and class implementations. These languages (and perhaps others) preserve type information for functions in the object code
Object file
An object file is a file containing relocatable format machine code that is usually not directly executable. Object files are produced by an assembler, compiler, or other language translator, and used as input to the linker....

, allowing the linker to verify that functions are used correctly.

COBOL
COBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....

 and RPG IV have a form of include files called copybooks
Copybook (programming)
A copybook is a section of code written in a high-level programming language or assembly language that can be copied and inserted into several different programs...

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

Fortran
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...

 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 (usually *.mod) files in a compiler-dependent format, although some compilers have placed this information directly into object files. Note that 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.

See also

  • Include directive
  • One Definition Rule
    One Definition Rule
    The One Definition Rule is an important concept in the C++ programming language. It's defined in the ISO C++ Standard 2003, at section 3.2.- Summary :In short the ODR states that:...

  • Application Programming Interface
    Application programming interface
    An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...

  • Interface Definition Language
  • #pragma once
    Pragma once
    In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation...


External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK