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
18
votes
1 answer

correct way to include .cpp and .h files in an Arduino sketch

First, the problem: main sketch file: char foo; // required to clean up some other problems #include // tried it in desperation, no help #include "a.h" void setup(){ Serial.begin(9600); Serial.println("\nTest begins"); …
Madivad
  • 2,999
  • 7
  • 33
  • 60
18
votes
3 answers

What is this C++ language construct: # (i.e. hash) integer "path_to_header_or_cpp_file" ?

I came across the following code in a .cpp file. I do not understand the construct or syntax which involves the header files. I do recognize that these particular header files relate to Android NDK. But, I think the question is a general question…
18
votes
3 answers

Why is there no multiple definition error when you define a class in a header file?

I'm not sure if I asked the question correctly, but let me explain. First, I read this article that explains the difference between declarations and definitions: http://www.cprogramming.com/declare_vs_define.html Second, I know from previous…
codecitrus
  • 659
  • 6
  • 17
17
votes
2 answers

Placing header files in a subdirectory of /usr/include with automake?

If I write a library, and have header files for development included, and have a src/Makefile.am like this: AM_CFLAGS = -std=c99 -Wall -Werror -Os lib_LTLIBRARIES = libmylibrary.la libmylibrary_la_SOURCES = a.c b.c include_HEADERS = a.h…
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
17
votes
7 answers

Is programming against interfaces in Java the same concept as using header files in C/C++?

The java code I'm working on at the moment has often a structure like file Controller.java: interface Controller {...} file ControllerImpl.java: class ControllerImpl implements Controller {...} But for every interface there is only one…
asmaier
  • 11,132
  • 11
  • 76
  • 103
17
votes
5 answers

Are there any performance implications to including every header?

Lets say I want to use hex() function. I know it is defined in header and I also know that it is included in header. The difference is that in are much more functions and other stuff I don't need. From a performance…
Martin Heralecký
  • 5,649
  • 3
  • 27
  • 65
17
votes
5 answers

Python.h header file missing on Mac OS X 10.6

I'm trying to access a shared C library in Python with ctypes on Mac OS X 10.6.8 with Python 2.7.4. To do this, I need to #include in my C code. If I try to compile a C script that only has that one include statement in it, call it…
Brett Morris
  • 791
  • 1
  • 4
  • 13
17
votes
2 answers

How to avoid #include dependency to external library

If I'm creating a static library with a header file such as this: // Myfile.h #include "SomeHeaderFile.h" // External library Class MyClass { // My code }; Within my own project I can tell the compiler (in my case, Visual Studio) where to look…
JBentley
  • 6,099
  • 5
  • 37
  • 72
17
votes
3 answers

C++ Header Files - What to include

I have written a very simple class in C++, namely it is the Rectangle class from http://www.cplusplus.com/doc/tutorial/classes/. In particular here's the content of the Header file (Rectangle.h): #ifndef RECTANGLE_H #define RECTANGLE_H class…
Pantelis Sopasakis
  • 1,902
  • 5
  • 26
  • 45
16
votes
2 answers

why is abs() and fabs() defined in two different headers in C

The standard library function abs() is declared in stdlib.h, while fabs() is in math.h. Why are they reside in different headers?
Sanjana Jose
  • 238
  • 1
  • 8
16
votes
2 answers

Auto generate header files for a C source file in an IDE

I am trying to use Eclipse and NetBeans for programming in C (not C++). Is there a feature/plugin for them which automatically keeps the source and header files in sync? As in, when I implement a function in the source file, does it automatically…
user2698
  • 315
  • 1
  • 3
  • 8
16
votes
4 answers

Why doesn't Qt Creator find included headers in included paths - even though qmake is able to find them

I joined an already existing (opensource-) Qt 4 project to add some functionality. The project compiles and runs perfectly on Linux Slackware with Qt 4.8.5. As IDE I first used KDevelop (comes with Slackware), but Qt project files aren't supported…
user3122343
  • 161
  • 1
  • 1
  • 4
16
votes
4 answers

Automatically split (refactor) .h into header and implementation (h+cpp)

When writing C++ code, I often start by writing full 'implementation' code in my header files, then later need to refactor the implementation into a .cpp file. This is great, but I find this process laborious, but otherwise pretty easy, so I…
aaaidan
  • 7,093
  • 8
  • 66
  • 102
15
votes
2 answers

"Multiple definition of" "first defined here" on GCC 10.2.1 but not GCC 8.3.0

I've had a bit of a look around Stackoverflow and the wider Internet and identified that the most common causes for this error are conflation of declaration (int var = 1;) and definition (int var;), and including .c files from .h files. My small…
i336_
  • 1,813
  • 1
  • 20
  • 41
15
votes
4 answers

C functions without header files

This should be very trivial. I was running through a very basic C program for comparing strings: #include int strcmp(char *s, char *t); int main() { printf("Returned: %d\n", strcmp("abc", "adf")); return 0; } int strcmp(char *s,…
M.K.
  • 391
  • 2
  • 7