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
13
votes
8 answers

Is reducing number of cpp translation units a good idea?

I find that if there are a lot of classes the compilation time is dramatically increased when I use one *.h and one *.cpp file per class. I already use precompiled headers and incremental linking, but still the compile time is very long (yes I use…
Alex Jenter
  • 4,324
  • 4
  • 36
  • 61
13
votes
9 answers

Why is including a header file such an evil thing?

I have seen many explanations on when to use forward declarations over including header files, but few of them go into why it is important to do so. Some of the reasons I have seen include the following: compilation speed reducing complexity of…
Jon
  • 363
  • 3
  • 13
13
votes
2 answers

Global variables in header only library

I'm writing a header-only logger library and I need global variables to store current logger settings (output flags, log file descriptor etc.). My thoughts: I can't declare variables as extern, as i don't have access to the translation units to…
user4380604
13
votes
2 answers

What is the difference between #import and #include in C?

I've read up a bit on preprocessor directives and I've seen #import being used a few times in C programs. I'm not sure what the difference is between them, some sites have said that #include is only used for header files and #import is used more in…
AkThao
  • 582
  • 4
  • 7
  • 23
13
votes
2 answers

Can a class name be used as a namespace?

I remember being told that C++ classes have their own namespaces, and that the class name could be used as a namespace for scope resolution, like this: // Example.h class Example { void Private(); public: void Public(); } and, later in a…
Stefan Stanković
  • 628
  • 2
  • 6
  • 17
13
votes
4 answers

C++ program using a C library headers is recognizing "this" as a keyword. Extern "C" error?

My C++ program needs to use an external C library. Therefore, I'm using the extern "C" { #include } syntax for every module I need to use. It worked fine until now. A module is using the this name for some variables in one of…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
12
votes
4 answers

Include File Ordering Strategy

I've seen fairly consistent advice that an implementation file (.cc / .cpp) should include its corresponding class definition file first, before including other header files. But when the topic shifts to header files themselves, and the order of…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
12
votes
3 answers

javac "no source files" when using -h option

I'm trying to experiment with using the JNI and JDK 9. I have a class NativeTest.java that looks like this: public class NativeTest { static { System.loadLibrary("hello"); } private native void sayHello(); public static…
David Mordigal
  • 813
  • 2
  • 9
  • 22
12
votes
1 answer

Why /usr/include/linux/stddef.h is empty?

This header file shall define NULL or size_t and other macros, but I find that /usr/include/linux/stddef.h is empty? Why?
Asuka
  • 301
  • 4
  • 16
12
votes
5 answers

Should '#include' and 'using' statements be repeated in both header and implementation files (C++)?

I'm fairly new to C++, but my understanding is that a #include statement will essentially just dump the contents of the #included file into the location of that statement. This means that if I have a number of '#include' and 'using' statements in my…
David Mason
  • 2,917
  • 4
  • 30
  • 45
12
votes
4 answers

How to install/locate R.h and Rmath.h header files?

I am trying to compile some C code (called rand_beta) in terminal which contains the lines to include R.h and Rmath.h header files using gcc -o rand_beta rand_beta.c so I can then call the code from within R. However, I get the error messages:…
user3018658
  • 121
  • 1
  • 1
  • 3
12
votes
2 answers

instance variables in @interface; header vs implementation

Is there any difference between declaring a private instance variable in the header vs declaring it in the implementation? in TestObj.h @interface TestObj : NSObject { int test; } @end vs in TestObj.m @interface TestObj() { int…
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
12
votes
3 answers

When should I use -inl.h files?

I just noted this item in the Google C++ Coding Style Guide - and I don't quite get it. If I put an inline method or function in a file other than the header included by other files, it will not be a method of the class; and it will only be usable…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
12
votes
4 answers

Difference Between includes and imports

Possible Duplicate: What is the difference between #import and #include in Objective-C? What is the difference between #include< > #include" " #import< > #import" "
Gugan
  • 1,625
  • 2
  • 27
  • 65
11
votes
2 answers

inclusion of header files -- relative to current directory or to include directories?

I have the following direcory stucture: src +-- lib1 +-- lib1.h +-- lib2 +-- lib2.h Both lib1 and lib2 are gonna be distributed (installed). lib2 makes use of lib1, so it needs some includes: #include "../lib1/lib1.h" // 1 #include…
wal-o-mat
  • 7,158
  • 7
  • 32
  • 41