Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header files and include best practice

I have a quick question regarding header files, include statements, and good coding style. Suppose I have 2 classes with associated source and header files, and then a final source file where main() is located.

Within Foo.hpp I have the following statements:

#include <string>
#include <iostream>
#include <exception>

Now withing Bar.hpp I have the following statements:

#include "Foo.hpp"
#include <string>

And finally withing Myprogram.cpp I have the following statements:

#include "Bar.hpp"
#include <string>
#include <iostream>
#include <exception>

I know the include statements in <> in Myprogram.cpp and Bar.hpp aren't necessary for the program to compile and function, but what is the best practice or right way of doing things? Is there any reason to not explicitly include the necessary header files in each file?

like image 538
boulderprog Avatar asked Mar 14 '13 21:03

boulderprog


People also ask

What should be included in a header file?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .

Should I put include in header file?

A header file should be included only when a forward declaration would not do the job. The header file should be so designed that the order of header file inclusion is not important.

Where should you include header files?

Header files should #include the minimum header files necessary, and source files should also, though it's not as important for source files. The source file will have the headers it #include s, and the headers they #include , and so on up to the maximum nesting depth.

What happens when you include a header file?

It attempts to find a function definition (implementation), which exactly matches the header you declared earlier. What happens if you #include header file is that compiler (specifically, the preprocessor) copies the whole contents of header file into place, where you put your #include .


2 Answers

You should include all necessary files in every file that needs them. If MyProgram.cpp needs string, include it, instead of relying on it being included by Bar.hpp. There's no guarantee 2 weeks from now Bar.hpp will still include it, and then you'll be left with compiler errors.

Note the necessary - i.e. make sure you actually need an include, when a forward declaration or even leaving it out completely will do.

Also, note that some system headers might include others - apart from a few exceptions, there's no requirement. So if you need both <iostream> and <string> include both, even if you can compile only with one of them.

The order in which the includes appear (system includes vs user includes) is up to the coding standard you follow - consistency is more important than the choice itself.

like image 52
Luchian Grigore Avatar answered Sep 25 '22 16:09

Luchian Grigore


Include all you need in every file, but do not include any file that you do not need. Normally, it is the job of the included file to make sure it is not included twice, using precompiler flags, etc...

For example if is needed by Foo.cpp, but not by Foo.h, include it in Foo.cpp not in Foo.h. If required in both, include in both.

Tangentially, as a best practice, never use using directives in a header file. If you need you can use using directives in implementation files (.cpp).

like image 29
meyumer Avatar answered Sep 21 '22 16:09

meyumer