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

What exactly do C include guards do?

Let's say I have a header file "header.h" with a function definition. #ifndef HEADER_FILE #define HEADER_FILE int two(void){ return 2; } #endif This header file has an include guard. However, I'm kind of confused as to what #define HEADER_FILE is…
Izzo
  • 4,461
  • 13
  • 45
  • 82
26
votes
3 answers

What is an Erlang hrl file?

What is an .hrl file in Erlang/OTP? Is it some sort of library mechanism?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
26
votes
2 answers

Where are the opencv2 include files?

I'm struggling to find the include files that should be in a directory names "opencv2"! I've downloaded OpenCV-2.4.0.tar.bz2 from here and extracted the files and ran cmake, which seemed to build the library successfully. The problem is that under…
James Bedford
  • 28,702
  • 8
  • 57
  • 64
25
votes
1 answer

How to use namespace across several files

I notice that C++'s std namespace is spread across several files (like in vector, string, iostream, etc.). How can I accomplish the same thing in my programs? Do I simply declare the same namespace in each individual header file, so that it's…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
25
votes
6 answers

Are there tools that help organizing #includes?

Are there any tools that help organizing the #includes that belong at the top of a .c or .h file? I was just wondering because I am reorganizing my code, moving various small function definitions/declarations from one long file into different…
Frank
  • 64,140
  • 93
  • 237
  • 324
24
votes
4 answers

Include .cpp instead of header(.h)

There are some cases when we include .cpp file instead of standard header file (.h), for example: #include "example.cpp" instead of #include "example.h" It seems to work but is this safe or should I avoid it? What about the compilation time?
sysop
  • 800
  • 2
  • 9
  • 18
24
votes
5 answers

endian.h not found on mac osx

I meet some trouble when I compile some C code on my mac which give me this error : fatal error: 'endian.h' file not found I did some google search about this problem.It seems like mac os x does not have header files like "endian.h", we have…
geasssos
  • 419
  • 1
  • 5
  • 10
24
votes
3 answers

Error "C++ requires a type specifier for all declarations whilst defining methods"

I'm relatively new to C++ (so try and keep answers simple please!), and I can't understand why I get the error: C++ requires a type specifier for all declarations whilst defining methods. I am trying to write a simple program to read a text file…
Lorienas
  • 279
  • 2
  • 3
  • 7
23
votes
3 answers

small functions defined in header files: inline or static?

I have a number of small functions which are defined in a .h file. It is a small project (now) and I want to avoid the pain of having declarations and definitions separate, because they change all the time. To avoid multiply-defined symbols, I can…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
23
votes
3 answers

How to use the header

In a related question ("std::string formatting like sprintf") I learned about this awesome new C++20 header . However, there seems to be no supporting compiler. Is this correct or is there a way to use it anyway? I'm using g++ 9.3 with the…
infinitezero
  • 1,610
  • 3
  • 14
  • 29
23
votes
3 answers

No warning with uninitialized C string

I'm currently wondering why I don't get an error from GCC during compilation/linking of a small C program. I declared in version.h the following string: const char* const VERSION; In version.c I have set the initialization of the variable: const…
23
votes
5 answers

Best practices for use of C++ header files

I have the following doubts on header files usage. 1 - Include guards placing after comments /* Copyright Note and licence information (multiple lines) */ #ifndef FOO_H #define FOO_H // Header file contents #endif Herb Sutter says in his "C++…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
23
votes
5 answers

How to include header files

Does it matter how I import header files? I've seen double quotes as well as arrows used. #include #include "Some_Header.h" Does it matter if they're capitalized a certain way as well? Experimenting around with this, it seems neither…
arjs
  • 2,835
  • 4
  • 22
  • 18
23
votes
1 answer

Including header file from static library

I am making a test setup of a C static library and program. The library code, located in a subdirectory 'foo' of my project, contains the following files: foo/foo.c: #include void foo(void) { printf("something"); } foo/foo.h: #ifndef…
Ethan McTague
  • 2,236
  • 3
  • 21
  • 53
23
votes
2 answers

Should I define static inline methods in header file?

I've read about how it is usually best not to define anything in header files because redundant copies are made for every other file that includes the header file. However, in the case of static inline methods, it appears that I have to define it on…
Some Newbie
  • 1,059
  • 3
  • 14
  • 33