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
30 answers

What is the difference between #include and #include "filename"?

What is the difference between using angle brackets and quotes in an include directive? #include #include "filename"
  • 49,608
  • 5
  • 21
  • 14
9 answers

Why have header files and .cpp files?

Why does C++ have header files and .cpp files?
6 answers

.c vs .cc vs. .cpp vs .hpp vs .h vs .cxx

Possible Duplicates: *.h or *.hpp for your class definitions Correct C++ code file extension? .cc vs .cpp I used to think that it used to be that: .h files are header files for C and C++, and usually only contain declarations. .c files are C…
  • 205,094
  • 128
  • 528
  • 886
17 answers

Is it a good practice to place C++ definitions in header files?

My personal style with C++ has always been to put class declarations in an include file and definitions in a .cpp file, very much like stipulated in Loki's answer to C++ Header Files, Code Separation. Admittedly, part of the reason I like this style…
  • 44,016
  • 10
  • 73
  • 134
14 answers

Why should I not include cpp files and instead use a header?

So I finished my first C++ programming assignment and received my grade. But according to the grading, I lost marks for including cpp files instead of compiling and linking them. I'm not too clear on what that means. Taking a look back at my code, I…
  • 8,510
  • 4
  • 36
  • 48
5 answers

Header files for x86 SIMD intrinsics

Which header files provide the intrinsics for the different x86 SIMD instruction set extensions (MMX, SSE, AVX, ...)? It seems impossible to find such a list online. Correct me if I'm wrong.
  • 256,549
  • 94
  • 388
  • 662
2 answers

Is #pragma once part of the C++11 standard?

Traditionally, the standard and portable way to avoid multiple header inclusions in C++ was/is to use the #ifndef - #define - #endifpre-compiler directives scheme also called macro-guard scheme (see code snippet below). #ifndef…
  • 41,839
  • 11
  • 94
  • 168
9 answers

"using namespace" in c++ headers

In all our c++ courses, all the teachers always put using namespace std; right after the #includes in their .h files. This seems to me to be dangerous since then by including that header in another program I will get the namespace imported into my…
  • 20,590
  • 28
  • 126
  • 201
12 answers

What should go into an .h file?

When dividing your code up into multiple files, what exactly should go into an .h file and what should go into a .cpp file?
Enrico Tuvera Jr
  • 2,739
  • 6
  • 35
  • 51
7 answers

How can a C++ header file include implementation?

Ok, not a C/C++ expert by any means, but I thought the point of a header file was to declare the functions, then the C/CPP file was to define the implementation. However, reviewing some C++ code tonight, I found this in a class's header…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
6 answers

C++: Namespaces -- How to use in header and source files correctly?

Consider a pair of two source files: an interface declaration file (*.h or *.hpp) and its implementation file (*.cpp). Let the *.h file be like the following: namespace MyNamespace { class MyClass { public: int foo(); }; } I have seen two…
  • 3,643
  • 3
  • 32
  • 40
10 answers

Makefile, header dependencies

Let's say I have a makefile with the rule %.o: %.c gcc -Wall -Iinclude ... I want *.o to be rebuilt whenever a header file changes. Rather than work out a list of dependencies, whenever any header file in /include changes, then all objects in the…
  • 58,961
  • 76
  • 175
  • 221
3 answers

Default member values best practice

Is it good practice when writing C++11 code to set default values for class members in the header file of the class? Or is it better to do this in the constructor of the class? EDIT: I mean: foo.h: #include using std::string; class Foo{ …
  • 20,883
  • 7
  • 57
  • 74
4 answers

Where to find the complete definition of off_t type?

I am sending file from client to server using TCP. To mark the end of the file I like to send file size before the actual data. So I use stat system call to find the size of the file. This is of type off_t. I like to know how many bytes it occupies…
  • 2,298
  • 5
  • 31
  • 34
4 answers

Where is the header file on Linux? Why can't I find ?

Possible Duplicate: How to implement getch() function of C in Linux? What is the equivalent Linux version of the conio.h header file from MS-DOS? Is there a way to replace its functionality? e.g. getch() I'm using gcc and the text editor Geany…
  • 2,942
  • 4
  • 23
  • 36
2 3
99 100