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
40
votes
3 answers

How to use C code in C++

Just a small question: Can C++ use C header files in a program? This might be a weird question, basically I need to use the source code from other program (made in C language) in a C++ one. Is there any difference between both header files in…
SadSeven
  • 1,247
  • 1
  • 13
  • 20
39
votes
5 answers

Header guards in C++ and C

At LearnCpp.com | 1.10 — A first look at the preprocessor. Under Header guards, there are those code snippets: add.h: #include "mymath.h" int add(int x, int y); subtract.h: #include "mymath.h" int subtract(int x, int y); main.cpp: #include…
Simplicity
  • 47,404
  • 98
  • 256
  • 385
39
votes
2 answers

Which headers in the C++ standard library are guaranteed to include another header?

The C++ standard library headers may include each other in unspecified ways, so programmers generally shouldn't depend on one header including another. In a few cases, however, a header is guaranteed to include another header, or make available…
T.C.
  • 133,968
  • 17
  • 288
  • 421
39
votes
8 answers

C header issue: #include and "undefined reference"

I have three files, main.c, hello_world.c, and hello_world.h. For whatever reason they don't seem to compile nicely, and I really just can't figure out why... Here are my source files. First hello_world.c: #include #include…
user1018501
  • 493
  • 1
  • 4
  • 4
38
votes
5 answers

Is is a good practice to put the definition of C++ classes into the header file?

When we design classes in Java, Vala, or C# we put the definition and declaration in the same source file. But in C++ it is traditionally preferred to separate the definition and declaration in two or more files. What happens if I just use a header…
sepisoad
  • 2,201
  • 5
  • 26
  • 37
38
votes
6 answers

Where are the headers of the C++ standard library

I wonder where on my file system I find the headers of the C++ Standard library. In particular I am looking for the definition of the vector template. I searched in /usr/include/ and various subdirectories. I also tried 'locate vector.h' which…
Thomas
  • 2,093
  • 3
  • 23
  • 28
36
votes
7 answers

C++, how to declare a struct in a header file

I've been trying to include a structure called "student" in a student.h file, but I'm not quite sure how to do it. My student.h file code consists of entirely: #include using namespace std; struct Student; while the student.cpp file…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
33
votes
6 answers

Include in header file vs. forward-declare and include in .cpp

I have a class B, and I want to call members from class A. So: 1. //A.h class B; class A { private: B* m_p; }; //a.cpp #include "B.h" 2. // A.h #include "B.h" class A { private: B * impl_; }; Which way is better and is…
colddie
  • 1,029
  • 1
  • 15
  • 28
33
votes
6 answers

C++: Reason why using ".hh" as extension for C++ header files

I would like to know why we use ".hh" as extension for C++ header files instead of using just ".h". The header files are preprocessed and the preprocessor doesn't even care about the extension of the header file. So, even if I create a header file…
veda
  • 6,416
  • 15
  • 58
  • 78
32
votes
8 answers

Self-sufficient header files in C/C++

I recently posted a question asking for what actions would constitute the Zen of C++. I received excellent answers, but I could not understand one recommendation: Make header files self-sufficient How do you ensure your header files are…
Escualo
  • 40,844
  • 23
  • 87
  • 135
31
votes
4 answers

How do I run the preprocessor on local headers only?

I want the preprocessor to read in the includes of local headers, but ignore the includes of system headers. To put it another way, how do I get the preprocessor to skip over preprocessing directives of the form: #include
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
30
votes
2 answers

Why include related header first?

On the question C/C++ include file order/best practices, the best answer recommends to include the related header first. Same for Google and Mozilla style guides. However, in both cases, I couldn't find a good reason why you would do this. Google…
Arnaud
  • 536
  • 5
  • 13
30
votes
5 answers

Should I include stddef.h or cstddef for size_t

When I want to use size_t in C++, should I include or ? I have heard several people saying that was a bad idea, and it should be deprecated. Why is that?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
30
votes
2 answers

How to automatically generate function headers for .h file in Clion?

When writing a library in C, I usually end up implementing functions before defining them in the corresponding header file. As copying function header's in the header file is a repetitive task, I was wondering if I can automatically generate…
Ali Alavi
  • 2,367
  • 2
  • 18
  • 22
30
votes
4 answers

What is WINVER?

I was looking at some code and they had this line: #define WINVER 0x0501 in stdafx.h file? Why do you need to define WINVER? How does it affect your code? Can someone please explain?
aajkaltak
  • 1,437
  • 4
  • 20
  • 28