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
votes
6 answers

Installation of Visual Studio 2010 (any edition) installs only 2 files in the C++ headers directory

I installed Visual Studio 2010 Premium on my Windows 7 workstation. After loading a test C++ project, I noticed that it could not locate iostream. I took a look in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include and noticed that…
30
votes
2 answers

Do template specialisations belong into the header or source file?

During compilation I get a "multiple definition" error, which refers to a template specialisation in a header file. Do I need to put the specialisations into the source file?
Michael
  • 1,464
  • 1
  • 20
  • 40
29
votes
5 answers

How to define an array of strings of characters in header file?

I have many different 3 axis sensors I am writing test code for. In the C files for each of them, I have the same char string defined: char axis[3][8] = {"X", "Y", "Z"} which I use when I "for" loop results to print the axis that is failing like…
user1054210
  • 481
  • 1
  • 5
  • 11
29
votes
4 answers

When are header-only libraries acceptable?

Personally, I quite like header-only libraries, but there are claims they cause code bloat due to over-inlining (as well as the other obvious problem of longer compile times). I was wondering, how much truth is there to these claims (the one about…
RaptorFactor
  • 2,810
  • 1
  • 29
  • 36
29
votes
3 answers

Why doesn't C# have header files? Will the namespace take care of everything?

Can anyone tell clearly about the usage of header files and namespaces in C#? Because in C++ I was using ******.h files to read library functions. And when I saw some sample programs in C# they were missing, Can anyone tell me why? I'm using C# to…
Senthur
  • 323
  • 1
  • 3
  • 6
28
votes
1 answer

What is the rationale for requiring inclusion of ?

From [dcl.init.list] The template std​::​initializer_­list is not predefined; if the header is not included prior to a use of std​::​initializer_­list — even an implicit use in which the type is not named — the program is…
Passer By
  • 19,325
  • 6
  • 49
  • 96
28
votes
2 answers

CUDA source files get a .cu extension. What do header files get?

The standard convention seems to be to give CUDA source-code files a .cu extension, to distinguish them from C files with a .c extension. What's the corresponding convention for CUDA-specific header files? Is there one?
Brooks Moses
  • 9,267
  • 2
  • 33
  • 57
28
votes
4 answers

Should variable definition be in header files?

My very basic knowledge of C and compilation process has gone rusty lately. I was trying to figure out answer to the following question but I could not connect compilation, link and pre-processing phase basics. A quick search on the Google did not…
Methos
  • 13,608
  • 11
  • 46
  • 49
28
votes
4 answers

@ sign in C variable declaration

I found this header file for PIC microcontrollers by the name of pic1250.h and I'm unable to get the hang of some syntax used in it. The source for the file is: /* * Header file for the Microchip * PIC 12c508 chip * PIC 12c509 chip * …
Avi Gabhawala
  • 291
  • 3
  • 5
28
votes
6 answers

xCode 4.4 does not get all the .pch file headers imports?

This is my .pch file - // // Prefix header for all source files of the 'English Club' target in the 'English Club' project // #import #ifndef __IPHONE_4_0 #warning "This project uses features only available in iOS SDK 4.0 and…
shannoga
  • 19,649
  • 20
  • 104
  • 169
28
votes
2 answers

CMake - dependencies (headers) between apps/libraries in same project

I have the following project structure: CMakeLists.txt lib1/CMakeLists.txt and all cpp and header files of the lib lib2/CMakeLists.txt and all cpp and header files of the lib app/CMakeLists.txt and all cpp and header files of the app The main…
Ela782
  • 5,041
  • 5
  • 53
  • 66
27
votes
8 answers

Header file inclusion static analysis tools?

A colleague recently revealed to me that a single source file of ours includes over 3,400 headers during compile time. We have over 1,000 translation units that get compiled in a build, resulting in a huge performance penalty over headers that…
fbrereto
  • 35,429
  • 19
  • 126
  • 178
27
votes
2 answers

Declaring variables in header files C++

I'm trying to create a simple program that takes input from the user in C++ using good programming practices. It consists of Input.hpp, Input.cpp and main.cpp. I keep getting a multiple definition error even though I am using ifndef to prevent that.…
Kareem Aboughazala
  • 535
  • 1
  • 6
  • 15
27
votes
3 answers

Static const variable declaration in a header file

If I declare static const variable in header file like this: static const int my_variable = 1; and then include this header in more than one .c files, will compilator make new instance per each file or will be "smart" enough to see it is const and…
Michał
  • 691
  • 1
  • 5
  • 22
27
votes
1 answer

sql.h header file missing though unixODBC is installed

I am on an up-to-date Ubuntu 12.04 system. I have unixodbc (v2.2.14 from ubuntu repos), MySQL and its relevant drivers installed. Also connected to a valid DSN. Verified by issuing isql DBName UName passwd. I am trying to compile a C application…
Richard
  • 528
  • 1
  • 5
  • 15