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
49
votes
11 answers

C++ namespace and include

Why do we need both using namespace and include directives in C++ programs? For example, #include using namespace std; int main() { cout << "Hello world"; } Why is it not enough to just have #include or just have using…
duli
  • 1,621
  • 3
  • 16
  • 25
49
votes
6 answers

Including header files in C/C++ more than once

Is it ever useful to include a header file more than once in C or C++? If the mechanism is never used, why would the compiler ever worry about including a file twice; if it really were useless, wouldn't it be more convenient if newer compilers made…
math4tots
  • 8,540
  • 14
  • 58
  • 95
48
votes
3 answers

Defining constructor in header file vs. implementation (.cpp) file

I can define the body of a class constructor in the class .h file or in the implementation file .cpp. These two styles are probably identical as far as the compiler is concerned within a specific project (project for me means DLL). Same applies to…
Andrea
  • 735
  • 2
  • 8
  • 12
48
votes
3 answers

error C2039: 'string' : is not a member of 'std', header file problem

I am having problems with a class I am writing. I have split the class into a .h file that defines the class and an .cpp file that implements the class. I receive this error in Visual Studio 2010 Express: error C2039: 'string' : is not a member of…
Calzone41
  • 491
  • 1
  • 4
  • 5
47
votes
7 answers

C/C++ header and implementation files: How do they work?

How does the main function know about function definitions (implementations) in a different file? For example, say I have 3 files: //main.cpp #include "myfunction.hpp" int main() { int A = myfunction( 12 ); ... } //myfunction.cpp #include…
nickelpro
  • 2,537
  • 1
  • 19
  • 25
47
votes
6 answers

Is the backslash acceptable in C and C++ #include directives?

There are two path separators in common use: the Unix forward-slash and the DOS backslash. Rest in peace, Classic Mac colon. If used in an #include directive, are they equal under the rules of the C++11, C++03, and C99 standards?
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
47
votes
4 answers

What is the point of header files in C?

Possible Duplicates: [C] Header per source file. In C++ why have header files and cpp files? C++ - What should go into an .h file? Is the only reason header files exist in C so a developer can quickly see what functions are available and what…
alex
  • 479,566
  • 201
  • 878
  • 984
47
votes
17 answers

How to make Xcode find file FacebookSDK.h?

It says "FacebookSDK/FacebookSDK.h file not found" Yet I can jump-to-definition on the #import and it takes me to the file. And once I added the #import it now knows what FBFriendPickerDelegate is and it now doesn't have an error on that line. I…
Curtis
  • 2,486
  • 5
  • 40
  • 44
46
votes
10 answers

Where to document functions in C or C++?

I have a C program with multiple files, so I have, for example, stuff.c which implements a few functions, and stuff.h with the function prototypes. How should I go about documenting the functions in comments? Should I have all the docs in the header…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
46
votes
4 answers

multiple definition in header file

Given this code sample: complex.h : #ifndef COMPLEX_H #define COMPLEX_H #include class Complex { public: Complex(float Real, float Imaginary); float real() const { return m_Real; }; private: friend std::ostream&…
Jérôme
  • 26,567
  • 29
  • 98
  • 120
43
votes
5 answers

Including C headers inside a C++ program

I have a C++ program (.cpp) inside which I wish to use some of the functions which are present inside the C header files such as stdio.h, conio.h, stdlib.h, graphics.h, devices.h etc. I could include the stdio.h library inside my cpp file as :…
Arjun Vasudevan
  • 792
  • 4
  • 13
  • 33
43
votes
5 answers

List of standard header files in C and C++

Where could I find the list of all header files in C and C++? While I am building a library, I am getting an error like 'tree.h not found'. I suppose this is a standard header file in C and C++. This raised in me the curiosity to know all the…
Vijay
  • 65,327
  • 90
  • 227
  • 319
42
votes
2 answers

Difference between cuda.h, cuda_runtime.h, cuda_runtime_api.h

I'm starting to program with CUDA, and in some examples I find the include files cuda.h, cuda_runtime.h and cuda_runtime_api.h included in the code. Can someone explain to me the difference between these files?
Renan
  • 1,910
  • 4
  • 22
  • 36
41
votes
2 answers

How to have CMake show headers-that are not part of any binary target-in the IDE?

In our workflow, we can have a module A that is composed of several header files, module A not producing any binary (side note: it will obviously be used by other modules, that include some of the headers from module A to produce binaries). A good…
Ad N
  • 7,930
  • 6
  • 36
  • 80
40
votes
4 answers

Header file included only once in entire program?

I know this is a common question but I still can't fully get my head around it. In a C or C++ program generated from multiple different source and header files, will each header file be only included once in the entire code when the header guards…
Engineer999
  • 3,683
  • 6
  • 33
  • 71