1. Technology

Definition of Header File

By

Definition: In C and C++, applications are divided into smaller parts through the use of header files.

Any function prototypes, and type definitions that can be exported from a source code file are put in a header file. From the point of view of the main application, these functions are external.

The compiler reads the included header file and knows where a particular function comes from. Without this it would just report an undefined function error.

When the application is linked, all calls to external functions are resolved by the linker and built into an exe or dll (.so on Linux).

Dangers

Because file_b might include file_a and both include file_d, it's important to protect against this by using the #ifndef directive to check that file_d is only included once. So the 'include for file_d should be wrapped inside a #ifndef ... #endif and these four lines should be in both file_a and file_b.

#ifndef FILE_D
#define FILE_D
#include <file_d.h>
#endif

If the pre-processor includes file_a first then the FILE_D will not exist and so it will be defined and file_d.h will be included. Next when file_b is processed, FILE_D will exist and so file_d.h will not be included for a second time.

Glossary:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Examples:
Moving
  1. About.com
  2. Technology
  3. C / C++ / C#
  4. Glossary
  5. Header File - C / C++ / C#

©2014 About.com. All rights reserved.