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

undefined reference to

I have this simple test file: #include "stack.h" int main() { Stack* stck = init_stack(); return 0; } and stack.h is defined as follows: #ifndef STACK_H #define STACK_H #define EMPTY_STACK -1 typedef struct stack { char ch; struct stack*…
user1508893
  • 9,223
  • 14
  • 45
  • 57
19
votes
5 answers

Xcode Not Immediately Recognizing New Classes (iOS)

I've been working with Xcode for about 5 months now and I just recently ran across a problem when I add a new class. If I add a new class, say for example "CustomCell" and I try to import '#import CustomCell.h' into a different .m file it will give…
MillerMedia
  • 3,651
  • 17
  • 71
  • 150
19
votes
4 answers

putting function definitions in header files

If you want to put function definitions in header files, it appears there are three different solutions: mark the function as inline mark the function as static put the function in an anonymous namespace (Until recently, I wasn't even aware of…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
19
votes
3 answers

How to make Makefile recompile when a header file is changed?

I have written a Makefile to compile an openCV program on OSX (more general in Unix systems). The code has a header named constants.hpp where some constants are defined. I would like to make the Makefile recompile the program when this header file…
roschach
  • 8,390
  • 14
  • 74
  • 124
19
votes
1 answer

When to use .hpp files

I've created a library using C++, I want to create a Python Wrapper for this library and I am using boost.python - The problem is that I have created .h and .cpp files separately and for some reason, the .so file cannot link these .cpp files. I…
Phorce
  • 2,632
  • 13
  • 43
  • 76
19
votes
2 answers

Objective C - Error: 'Expected a type'

I'm getting a very strange error on something that I would have thought to be simple. #import #import "ViewController.h" #import "GameObject.h" @interface GameController : NSObject @property (strong) GLKBaseEffect *…
18
votes
5 answers

C++ - must friend functions be defined in the header file?

I want to overload the operator << in one of my classes. The signature goes like this: friend std::ostream& operator<<(std::ostream& os, const Annuaire& obj) When I try to define it in the .cpp file it says that the operator<< exactly takes 1…
Pacane
  • 20,273
  • 18
  • 60
  • 97
18
votes
3 answers

Why does Objective-C use header files instead of one-file classes like Java?

I primarily work in Java and am recently trying to learn Objective-C for Mac and iOS app development. Now, this language is quite different from what I'm used to, pointers, messages, etc, but I seem to be picking it up okay. This isn't a coding…
Dan2552
  • 1,244
  • 1
  • 15
  • 26
18
votes
4 answers

Eclipse CDT: Unresolved inclusion of stl header

I'm trying to use Eclipse to edit sources, compiled under C++ Builder, but stuck with Unresolved inclusion problem. For example, code like: #include Gives Unresolved inclusion: error in Eclipse IDE. C++ Builder indeed has no…
rmflow
  • 4,445
  • 4
  • 27
  • 41
18
votes
5 answers

How to fix "fatal error: opencv2/core.hpp: No such file or directory" for opencv4 installed in manjaro

Essentially, I've been able to install openCV fine for python but I also want to be able to do it for C++. I was able to install it using my linux distro's package manager (pacman for manjaro which is based on arch) but I haven't gotten the…
Notemaster
  • 465
  • 1
  • 3
  • 15
18
votes
5 answers

What is the reason for #pragma once inside header guards?

Just seen this inside #ifndef BOOST_ASIO_HPP #define BOOST_ASIO_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) /// .... #endif // BOOST_ASIO_HPP Disregarding the…
dubnde
  • 4,359
  • 9
  • 43
  • 63
18
votes
2 answers

Is there any reason to use extern "C" on headers without methods?

I frequently come across C header files that contain extern "C" guards, but don't contain any actual functions. For example: /* b_ptrdiff.h - base type ptrdiff_t definition header */ #ifndef __INCb_ptrdiff_th #define __INCb_ptrdiff_th #ifdef…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
18
votes
6 answers

What is the difference between header file and namespace?

I want to know the exact difference between Header file (as in MyHeader.hpp) and a namespace in c++?
2easylogic
18
votes
2 answers

Use of undeclared identifier in header file (Clang)

I am creating a function to read the contents of a file, located in an IO.cpp file: #include "IO.h" #include #include IO::IO() { //ctor } void IO::readFile(std::string fileName) { std::ofstream inputFile; …
Axmill
  • 305
  • 1
  • 2
  • 6
18
votes
3 answers

JDK 1.8 on Linux missing JNI include file

I am trying to compile the following project: https://github.com/entropia/libsocket-can-java I always get this error message? Does anyone know how to fix it, is it possibly a bug in JDK 1.8.0.11 on Linux (x64 Debian Wheezy)? In file included from…
arash javanmard
  • 1,362
  • 2
  • 17
  • 37