Questions tagged [header-files]

Header files are used in some programming languages to hold source code as a single copy that may be reused in multiple source files. This tag should be used for questions about using header files. A tag for the programming language such as C, C++, PHP, or Ruby should be used along with this tag since header file usage can vary between programming languages. This tag is not normally used with Java or Go programming and the import directive.

Header files are used in some programming languages to hold source code in a single container, the header file, that may be needed in multiple source files during the processing of the source files. The goal of a header file is to reduce duplication of the same source lines needed in multiple files. Instead of cloning the source lines into each of the files where they are needed, the source lines are put into a header file. The header file is then pulled into a source file by the compiler or script engine during processing of a source file.

The result is that only a single copy of the source lines needs to be maintained and the single copy of source can be reused in multiple files.

The primary purposes of header files is to make it easier to reuse functionality and to reduce errors caused by duplicated source code which becomes slightly different due to maintenance changes.

Typically a special directive such as include or require is used to insert a copy of the contents of the header file at a particular line of a source file during source processing by compiler or script engine. The concept is the include or require directive is replaced by the contents of the header file. Header files are used in a number of programming languages such as C and C++ as well as PHP and Ruby and Lua. The semantics of these directives will vary from programming language to programming language.

The import directive used in languages such as Java and Go is different from the include directive of C and C++ being more of a way to establish linkages and references between classes and packages as a part of combining software components (Java and Go) rather than the including of lines of source code (C and C++ and PHP).

Source lines typically put into header files are definitions and declarations that are needed in multiple files. Information may include class definitions or declarations, function prototypes or structure definitions/declarations or constants of various kinds.

Considerations for header files

When determining what source to put into a header file the primary rule of thumb is they should be lines of source that can be included in multiple files without causing a problem. For instance with the C programming language, a definition of a struct would be appropriate in a header file.

However putting the definition of a standard (not in-line) function into a C header file would not be appropriate. The problem with putting a standard C function definition into a header file is the function will be duplicated at each point where the header file is pulled into a source file. The result would be multiple definitions of the same function causing a linker error.

Some programming languages require care be taken that a header file is not included more than once. This can happen when several header files that are included into a source file themselves include still another header file. In the C and C++ programming languages macros or pragmas are used to enforce a once only rule. In other cases the source lines in the header file may not require a once only rule due to the nature of the source in the header file.

Care must also be taken that a dependency cycle is not created causing an infinite loop during the processing of header files. A header file may include other header files which end up including the original header file again causing a cycle or infinite loop until the compiler runs out of resources during header file processing.

PHP has the require_once directive which will perform a check if the header file has already been included or not.

In C and C++ an external utility, the Preprocessor, handles macro processing and other directives such as the include directive for header files generating a temporary file that is then compiled. In other languages the include directive processing is built into the compiler or script engine.

Typically a file extension of .h is used for header files. However for many programming languages this naming convention is not a requirement for correct processing.

3503 questions
65
votes
4 answers

default parameters in .h and .cpp files

COMPILER: g++ 4.7.2 Ok. So I am confused about default parameters in .h and .cpp files. It is mentioned in many places( including this site) that default parameters can be added only in .h files and not in .cpp files. However, this code proves it…
sudeepdino008
  • 3,194
  • 5
  • 39
  • 73
62
votes
2 answers

How to create two classes in C++ which use each other as data?

I'm looking to create two classes, each of which contains an object of the other class type. How can I do this? If I can't do this, is there a work-around, like having each class contain a pointer to the other class type? Thanks! Here's what I…
Steve Johnson
  • 949
  • 3
  • 12
  • 13
61
votes
1 answer

How to read a CMake Variable in C++ source code

I'd like to store the version number of my library in just one place. So I have defined such a variable in the CMake-file: SET(LIBINTERFACE_VERSION 1 CACHE INTEGER "Version of libInterface") With this definition I can generate a version.rc file…
Snowfox
  • 1,112
  • 1
  • 10
  • 12
60
votes
2 answers

How can I create C header files

I want to be able to create a collection of functions in a header file that I could #include in one of my C Programs.
user340838
  • 657
  • 1
  • 6
  • 3
59
votes
5 answers

scope of using declaration within a namespace

Is it safe (and correct) in a C++ header file to use the using declaration within a namespace as follows: #include namespace MyNamespace { using boost::numeric::ublas::vector; vector MyFunc(vector…
Brett Ryland
  • 1,045
  • 1
  • 9
  • 17
59
votes
18 answers

Pros & Cons of putting all code in Header files in C++?

You can structure a C++ program so that (almost) all the code resides in Header files. It essentially looks like a C# or Java program. However, you do need at least one .cpp file to pull in all the header files when compiling. Now I know some people…
user15071
  • 3,391
  • 8
  • 31
  • 31
59
votes
4 answers

How to make g++ search for header files in a specific directory?

I have a project (a library) that is subdivided into a few directories with code in them. I'd like to to have g++ search for header files in the project's root directory, so I can avoid different include paths for same header files across multiple…
corazza
  • 31,222
  • 37
  • 115
  • 186
57
votes
5 answers

Handling header files dependencies with cmake

I am using CMake on a small C++ project and so far it works great... with one twist :x When I change a header file, it typically requires recompiling a number of sources files (those which include it, directly or indirectly), however it seems that…
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
55
votes
3 answers

Is it a good practice to define C++ functions inside header files?

I'm wondering if it's a good practice to store C++ regular functions, not methods(the ones in classes) inside header files. Example: #ifndef FUNCTIONS_H_INCLUDED #define FUNCTIONS_H_INCLUDED int add(int a, int b) { return a + b; } #endif And…
Nobody
  • 605
  • 1
  • 7
  • 9
55
votes
4 answers

In what cases we need to include ?

In what cases should we include cassert?
Jane
  • 739
  • 1
  • 6
  • 10
54
votes
3 answers

What mean file with extension "h.in"?

I am studying the C language, and I saw a new extension that I had not seen before. What do files with the extension like library.h.in mean? Is it as the simple header with extension ".h"? What's the difference?
Anderson Carniel
  • 1,711
  • 2
  • 16
  • 22
52
votes
3 answers

Why do I see THROW in a C library?

When I do: less /usr/include/stdio.h (which is only a C library - nothing to do with C++) I see __THROW after quite a few function declarations. Also, comments above a few functions say that 'This function is a possible cancellation point and…
mynk
  • 1,194
  • 2
  • 13
  • 16
51
votes
9 answers

How do I include the string header?

I'm trying to learn about strings, but different sources tell my to include different headers. Some say to use , but others mention "apstring.h". I was able to do some basic stuff with apstring, but I've been told the other one is more…
Maxpm
  • 24,113
  • 33
  • 111
  • 170
50
votes
3 answers

Difference between iostream and iostream.h

What is the difference between iostream and iostream.h?
ckv
  • 10,539
  • 20
  • 100
  • 144
50
votes
6 answers

Initializing Constant Static Array In Header File

I have just found out that the following is not valid. //Header File class test { const static char array[] = { '1', '2', '3' }; }; Where is the best place to initialize this?
user174084
  • 1,087
  • 3
  • 14
  • 23