I have seen quite a few projects (often game engines) where all the header includes are placed in a single header file which sometimes contains macros etc as well e.g.
// Master.h
#include "header1.h"
#include "header2.h"
#include "header3.h"
.
.
#include "headerN.h"
Then when using code, the standard would be to only include Master.h file.
Other projects work on the basis that the source files should include only the headers they need.
What I want to know is if there is a definitive answer as to best practice, preferably with measurable results, or is it personal preference?
Yes, it's bad practice. It's also bad practice to include it in the corresponding header.
Unless you want the function to be inline , it is best to declare the function in the header and define it in a single source file and link it. If you declare the function as inline , then each of its function call in the source file will be replaced with the code inside the inline d function.
In a nutshell, include what you use (iwyu), introduced by Google, ensures source files include all headers used in the C++ code. This c++ include what you use methodology essentially maximizes the probability that code continues to compile even with reasonable changes made to the various interfaces.
To include a user defined header file one should use #include”name. h” i.e. enclosed within double quotes.
Most of the answers are mentioning compile time, and ignoring the fact that precompilation of headers has huge compile time benefits, and works much better with the master header technique.
Preferably, your headers should work if directly included without a master header file (this makes testing easier). Modern compilers have optimized the "skip header file if multiply included" logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With