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

Where are the C headers in MacOS Mojave?

It seems that Apple keeps on moving their tools around and the old solution of installing the command line tools are with using xcode-select --install doesn't work. In Mojave, xcode-select doesn't install anything anymore (the GUI always fails to…
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
15
votes
2 answers

include stdafx.h in header or source file?

I have a header file called stdafx.h and this one is precompiled of course. I've read that I should include these files into my .cpp files, but some of these statements are already needed in the header file coming with that. Should I add the stdafx…
Marnix
  • 6,384
  • 4
  • 43
  • 78
15
votes
2 answers

How to write a BitBake driver recipe which requires kernel source header files?

Introduction I have a do_install task in a BitBake recipe which I've written for a driver where I execute a custom install script. The task fails because the installation script cannot find kernel source header files within
karobar
  • 1,250
  • 8
  • 30
  • 61
15
votes
6 answers

fatal error C1014: too many include files : depth = 1024

I have no idea what this means. But here is the code that it supposely is happening in. //======================================================================================= // d3dApp.cpp by Frank Luna (C) 2008 All Rights…
numerical25
  • 10,524
  • 36
  • 130
  • 209
15
votes
3 answers

SSPI header file - fatal error

I get some fatal error on my project, the error is coming from sspi.h, i have to define something but i am not what and why, please someone explain. sspi.h(60): fatal error C1189: #error : You must define one of SECURITY_WIN32, SECURITY_KERNEL, or…
Cooker
  • 421
  • 1
  • 9
  • 19
15
votes
1 answer

Declare and initialize constant in header file

I'm well versed in the typical paradigm of: //.h extern const int myInt; //.c, .m, .cpp, what have you const int myInt = 55; But there's got to be a way to put that into .h files for use with libraries or other instances where you cannot access…
Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
15
votes
3 answers

Using a struct in a header file "unknown type" error

I am using Kdevelop in Kubuntu. I have declared a structure in my datasetup.h file: #ifndef A_H #define A_H struct georeg_val { int p; double h; double hfov; double vfov; }; #endif Now when I use it in my main.c file int…
dead_jake
  • 523
  • 2
  • 12
  • 30
14
votes
4 answers

Define Array in C

I have several 450 element character arrays (storing bitmap data to display on lcd screens.) I would like to put them under a header file and #define them, but I keep getting compilation errors. How would I do this in C? #define numbers[450] {0,…
Reid
  • 4,376
  • 11
  • 43
  • 75
14
votes
1 answer

Reorder function in c file based on c header file

Is there any tool to automatically reorder the .c file based on .h? For example, foo.h void function1(); void function2(); void function3(); And foo.c void function2(){} void function1(){} void function3(){} Can I reorder it as void…
Wei Shi
  • 4,945
  • 8
  • 49
  • 73
14
votes
6 answers

Include multiple header-files at once with only one #include-expression?

Is there any expression possible for the syntax to include multiple headers at once, with no need to write the "#include"-expression for each file new? Like, for example: #include , , , /* Dummy-Expression 1.…
14
votes
3 answers

Compiler not following symbolic links in Visual Studio C++

I am using Visual Studio 2008 C++ project (Visa 32 bit). I have the following #include directive in my source code. #include In my include path I specify the parent directory of 'example', i.e. C:/.../include where the full path…
Akusete
  • 10,704
  • 7
  • 57
  • 73
14
votes
3 answers

What if I need anonymous namespace in a header?

In C++ an anonymous namespace is equivalent to: namespace $$$$ { //something } using namespace $$$$; Where $$$$ is some kind of unique identifier. Anonymous namespace are then useful for code that should not be seen outside the compilation…
Paolo.Bolzoni
  • 2,416
  • 1
  • 18
  • 29
14
votes
4 answers

Multiple definition of const variables at header file

I defined some constants in flag.h so link.c and linkedlist.h can use it. But when I compile with: clang -Wall main.c link.c linkedlist.c I get the following /tmp/linkedlist-o2mcAI.o:(.rodata+0x0): multiple definition of…
Mateus Pires
  • 903
  • 2
  • 11
  • 30
14
votes
3 answers

Is an #include before #ifdef/#define Include-Guard okay?

I always placed my #include after the #ifdef/#define Include-Guard. Now the refactor mechanism of my IDE (Qt Creator) put it before the Include-Guard e.g. #include "AnotherHeader.h" #ifndef MYHEADER_H #define MYHEADER_H Can this cause any…
avb
  • 1,701
  • 5
  • 22
  • 37
14
votes
7 answers

Why don't C header files increase the binary's size?

I wrote the following C++ program class MyClass { public: int i; int j; MyClass() {}; }; int main(void) { MyClass inst; inst.i = 1; inst.j = 2; } and I compiled. # g++ program.cpp # ls -l…
PetrosB
  • 4,134
  • 5
  • 22
  • 21