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

What is python-dev package used for

I recently installed lxml. Before that, I had to install all the dependencies for that. So I tried to install liblxml2-dev, liblxslt1-dev and python-dev (google-searched for what packages are required for lxml) but even after that, I could not able…
Abhisek
  • 4,610
  • 3
  • 17
  • 27
86
votes
3 answers

Difference between @interface definition in .h and .m file

Normally we use @interface interface_name : parent_class { ...... } @end method in .h file and in .m file we synthesis the properties of variables declared in .h file. But in some code, this @interface.....@end method is kept in the .m…
Rajkanth
82
votes
7 answers

Objective C to Swift header file with multiple targets

I'm successfully calling my Swift classes from Objective C (for target 'MyApp') via the import statement: #import "MyApp-Swift.h" I've now created a new target called "MyAppLite" When I compile the new target, I get errors because "MyApp-Swift.h"…
Fittoburst
  • 2,215
  • 2
  • 21
  • 33
82
votes
3 answers

Why aren't my include guards preventing recursive inclusion and multiple symbol definitions?

Two common questions about include guards: FIRST QUESTION: Why aren't include guards protecting my header files from mutual, recursive inclusion? I keep getting errors about non-existing symbols which are obviously there or even weirder syntax…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
81
votes
2 answers

How to fix fatal error: zlib.h: no such file or directory?

I'm trying to compile miniSAT on Kali Linux 64 bits but I keep getting the error message: fatal error: zlib.h: no such file or directory I have read that there might be a linking problem that makes the compiler unable to find the header files, but…
user3043108
  • 965
  • 1
  • 8
  • 10
77
votes
10 answers

How can I find the header files of the C programming language in Linux?

When I write C programs in Linux, and then compile them using gcc, I am always curious about where those header files are. For example, where stdio.h is. More generally, where is stdbool.h? What I want to know is not only where it is, but also how…
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
76
votes
17 answers

Should C++ eliminate header files?

Many languages, such as Java, C#, do not separate declaration from implementation. C# has a concept of partial class, but implementation and declaration still remain in the same file. Why doesn't C++ have the same model? Is it more practical to…
Sasha
76
votes
9 answers

Tools to find included headers which are unused?

I know PC-Lint can tell you about headers which are included but not used. Are there any other tools that can do this, preferably on linux? We have a large codebase that through the last 15 years has seen plenty of functionality move around, but…
Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
74
votes
4 answers

How can I use the tool 'Include What You Use' together with CMake to detect unused headers?

The tool Include What You Use can be used to detect unneeded headers. I am using CMake for my C++ software project. How can I instruct CMake to run Include What You Use automatically on the source files of my software project?
Erik Sjölund
  • 10,690
  • 7
  • 46
  • 74
73
votes
5 answers

How do header and source files in C work?

I've perused the possible duplicates, however none of the answers there are sinking in. tl;dr: How are source and header files related in C? Do projects sort out declaration/definition dependencies implicitly at build time? I'm trying to understand…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
70
votes
3 answers

What's the differences between .dll , .lib, .h files?

Why in a project should I include some *.lib, .h or some other files? And what are these things used for?
MemoryLeak
  • 7,322
  • 23
  • 90
  • 133
69
votes
2 answers

undefined reference when calling inline function

I am getting a really odd error from GCC 4.8.1 with inline functions. I have two near-identical inline functions defined in header files (debug.h and error.h) in src/include/, with the only difference being what they print - one prefixes DEBUG: to…
Marco Scannadinari
  • 1,774
  • 2
  • 15
  • 24
68
votes
25 answers

Can I write C++ code without headers (repetitive function declarations)?

Is there any way to not have to write function declarations twice (headers) and still retain the same scalability in compiling, clarity in debugging, and flexibility in design when programming in C++?
thewreck
  • 721
  • 1
  • 5
  • 5
66
votes
10 answers

Difference between and ?

How come this code std::map m; m["a"]=1; compiles with (I'm using MSVC 2010) #include but not with #include ?
Valmond
  • 2,897
  • 8
  • 29
  • 49
65
votes
6 answers

Writing function definition in header files in C++

I have a class which has many small functions. By small functions, I mean functions that doesn't do any processing but just return a literal value. Something like: string Foo::method() const{ return "A"; } I have created a header file "Foo.h"…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184