Reputation: 2137
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?
Upvotes: 9
Views: 3809
Reputation: 163
I think that some questions in C++ - and also this one - you can answer yourself by remembering the C++ "mantra"
don't pay for what you don't use.
Save your users (i.e. the consumers of your code / library) computational cost (run-time and compile-time) wherever possible. don't pollute a namespace and throw more stuff at the compiler (also at the syntax highlighter etc) than needed. I think it's C++'s way of being polite :)
Upvotes: 0
Reputation: 2091
In my opinion, the 5 seconds you gain by not typing the name of the right header file are certainly not worth the potentially enormous increase of compile time induced by this method.
I'd say this is bad pratice.
However, as H2CO3 said, offering the possibility to use a master header file to the end-user of a framework can be quite helpful. GTK does that if I remember well.
Upvotes: 0
Reputation: 478
I would also add that including only needed headers when library provides a master one is rather bad idea. It often happens that such header does not include everything it needs, but rather depends on fact that master header will include all required headers before. So, if you are user of the library you usually do not have much choice and should follow the way suggested by it's author.
This is also why having master header may be considered as bad practice - it makes it harder to detect such case as I described above.
Upvotes: 1
Reputation: 283664
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.
Upvotes: 8
Reputation:
Since compiling C++ is expensive and can be particularly slow, I'd say you can avoid some extra pre-processing and parsing time by avoiding unnecessary inclusion of unused headers if there are a lot of header files (or there are a lot of implementation files which in turn include headers).
That's for the implementation of the library (you referring to game engines makes me think we're talking about libraries here). Now you can definitely make a "master" include file for convenience that those would include who use the library and want everything at one place (and don't have thousands of files at the same time).
Upvotes: 2
Reputation: 155
definitely a bad practice from the compilation time standpoint, since your project will have to be re-compiled from scratch everytime the file is modified or any of the included header is touched.
as a rule of thumb, you should include as few header as possible in your source files. However I can see some situations where this could come handy, with 3rd party libs that don't change very often
Upvotes: 2