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
22
votes
5 answers

Include std library in header or cpp?

If I have a class A which uses iostream, should I put the include statement of iostream in A.h or A.cpp?
user695652
  • 4,105
  • 7
  • 40
  • 58
22
votes
15 answers

Should every C or C++ file have an associated header file?

Should every .C or .cpp file should have a header (.h) file for it? Suppose there are following C files : Main.C Func1.C Func2.C Func3.C where main() is in Main.C file. Should there be four header files Main.h Func1.h Func2.h Func3.h Or there…
Vicky
  • 11,077
  • 11
  • 35
  • 29
22
votes
3 answers

What is the scope of a pragma directive?

What is the scope of a pragma directive? For example, if I say #pragma warning(disable: 4996) in a header file A that is included from a different file B, will that also disable all those warnings inside B? Or should I enable the warning at the end…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
22
votes
1 answer

How to make CLion use "#pragma once" instead of "ifndef ... def ..." by default for new header files?

By default, CLion will add the following lines to a newly created header file: #ifndef SOME_NAME_H #define SOME_NAME_H .... your code here #endif //SOME_NAME_H But I like #pragma once more. How can I configure CLion so that it uses #pragma once by…
a06e
  • 18,594
  • 33
  • 93
  • 169
22
votes
1 answer

Why cannot modern C++ IDEs auto-generate header files?

I understand the benefits and flexibility that header files provide, symbol discovery, the speed-up of processing for the compiler, etc. What I don't understand is why modern C++ IDE's don't auto-generate header files based on the members/methods…
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
22
votes
5 answers

Cross-Platform C++ code and single header - multiple implementations

I have heard that a way to write Cross Platform c++ code is to define classes as follows (for example, a Window class): window.h window_win32.cpp window_linux.cpp window_osx.cpp and then choose the implementation file accordingly. But what if i…
denidare
  • 474
  • 1
  • 4
  • 16
21
votes
2 answers

Ignore [clang-diagnostic-error] clang-tidy caused by 3rd party headers

I am using clang-tidy as a "linter" tool in development. I started to integrate 3rd party software into my code and when I include their header files using: -I/path/to/include tons of errors are generated, I haven't even #include the headers…
user2930353
  • 333
  • 1
  • 2
  • 6
21
votes
5 answers

Header files inclusion / Forward declaration

In my C++ project when do I have to use inclusion (#include "myclass.h") of header files? And when do I have to use forward declaration of the class (class CMyClass;)?
liaK
  • 11,422
  • 11
  • 48
  • 73
21
votes
6 answers

How do compilers know where to find #include ?

I am wondering how compilers on Mac OS X, Windows and Linux know where to find the C header files. Specifically I am wondering how it knows where to find the #include with the <> brackets. #include "/Users/Brock/Desktop/Myfile.h" // absolute…
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
21
votes
3 answers

What is the difference between - 1) Preprocessor,linker, 2)Header file,library? Is my understanding correct?

Okay, until this morning I was thoroughly confused between these terms. I guess I have got the difference, hopefully. Firstly, the confusion was that since the preprocessor already includes the header files into the code which contains the…
Paagalpan
  • 1,261
  • 2
  • 16
  • 25
20
votes
5 answers

What's the benefit for a C source file include its own header file

I understand that if a source file need to reference functions from other file then it needs to include its header file, but I don't understand why the source file include its own header file. Content in header file is simply being copied and pasted…
Chen
  • 326
  • 3
  • 12
20
votes
3 answers

Undef a typedef in C++?

I am working on a huge project which has one file A.h whose code has a line typedef unsigned __int16 Elf64_Half; Also since I am building on Linux and using dlinfo function, I have to include link.h file in my project. And this is where it…
voidMainReturn
  • 3,339
  • 6
  • 38
  • 66
20
votes
5 answers

Cannot open include file: 'atlbase.h': No such file or directory

Please have a look at the following code #define _ATL_APARTMENT_THREADED #include //You may derive a class from CComModule and use it if you want to override something, //but do not change the name of _Module extern CComModule…
PeakGen
  • 21,894
  • 86
  • 261
  • 463
20
votes
3 answers

using namespace std; in a header file

So, I have the following in a specification file #include #include using namespace std: class MyStuff { private: string name; fstream file; // other stuff public: void setName(string); } I also have in…
RebelPhoenix
  • 537
  • 1
  • 5
  • 14
20
votes
5 answers

including a header file twice in c++

What happens if I include iostream or any other header file twice in my file? I know the compiler does not throw error. Will the code gets added twice or what happens internally? What actually happens when we include a header file ?
user1743613