Jump to content

Header file: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Missed one of the tabs, sorry (8 space tab -> 4 spaces)
m +{{Redirect category shell}} using AWB
 
(20 intermediate revisions by 14 users not shown)
Line 1: Line 1:
#REDIRECT [[include directive]]
{{no footnotes|date=August 2012}}
In [[computer programming]], a '''header file''' is a file that allows programmers to separate certain elements of a program's [[source code]] into reusable files. Header files commonly contain [[forward declaration]]s of [[Class (computer science)|class]]es, [[subroutine]]s, [[variable (computer science)|variable]]s, and other [[identifier]]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]] and [[C++ standard library]] traditionally declare their standard functions in header files.


{{Redirect category shell|1=
More recently created compiled languages (such as [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]]) do not use forward declarations; identifiers are recognized automatically from [[source file]]s and read directly from [[dynamic library]] symbols. This means header files are not needed.
{{R from merge}}

}}
==Motivation==
In most modern computer programming languages, programmers can break up [[computer program|programs]] into smaller components (such as [[Class (computer science)|class]]es and [[subroutine]]s) and distribute those components among many [[Translation unit (programming)|translation units]] (typically in the form of source [[computer file|file]]s), which the system can [[compiler|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:
<source lang="c">
int add(int a, int b)
{
return a + b;
}
</source>
may be declared (with a [[function prototype]]) and then referred to in a second source file as follows:
<source lang="c">
int add(int, int);

int triple(int x)
{
return add(x, add(x, x));
}
</source>

One drawback of this method is that the [[function prototype|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. This process can be automated with the [[C preprocessor]] (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 (computer science)|parameter]] names are not used by the compiler, but they can be useful to programmers):

;File "add.h"
<source lang="C">
#ifndef ADD_H_GUARD
#define ADD_H_GUARD
int add(int a, int b);
#endif
</source>

This file uses [[include guard]]s to avoid multiple definitions of the function. The following code demonstrates how header files are used:

<source lang="C">
#include "add.h"
int triple(int x)
{
return add(x, add(x,x));
}
</source>

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 (programming language)|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. These languages (and perhaps others) preserve type information for functions in the [[object file|object code]], allowing the linker to verify that functions are used correctly.

[[COBOL]] and [[RPG IV]] have a form of include files called ''[[copybook (programming)|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 <code>COPY</code>, and replacement is done with a <code>REPLACING ... BY ...</code> clause.

[[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 (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]]
* [[Application Programming Interface]]
* [[Interface Definition Language]]
* [[Pragma once|#pragma once]]

==External links==
* [http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798 Organizing Code Files (the potential pitfalls and guidelines for using header files in C++)]
*[http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm C++ header file inclusion rules]

[[Category:C headers| ]]
[[Category:Source code]]
[[Category:C programming language]]
[[Category:C++]]

[[bg:Заглавен файл]]
[[cs:Hlavičkový soubor]]
[[de:Header-Datei]]
[[es:Archivo de cabecera]]
[[fa:فایل هدر]]
[[fr:Bibliothèque standard de C#Les en-têtes de la bibliothèque C ISO]]
[[ko:헤더 파일]]
[[hi:हेडर फ़ाइल]]
[[it:Header file]]
[[nl:Headerbestand]]
[[ja:ヘッダファイル]]
[[pl:Plik nagłówkowy]]
[[pt:Arquivo cabeçalho]]
[[ru:Заголовочный файл]]
[[sl:Zaglavna datoteka]]
[[sv:Headerfil]]
[[uk:Заголовний файл]]
[[zh:头文件]]

Latest revision as of 07:41, 5 June 2017

Redirect to: