Collecting headers into one header

Hello,

I'm writing a few libraries (statically and dynamically linked) for my own use. Each lib comes with quite a few header files and it's a little bit (!) annoying to include every single one when using functionallity of the lib.

So I wonder if I could just include all the header files in on "include" header which I will include when using the lib.

Does this have an impact on compile time? Are there other disadvangtages?

Thanks in advance!
So I wonder if I could just include all the header files in on "include" header which I will include when using the lib.


Yes. This is a very common technique.

Does this have an impact on compile time?


Yes. Including headers increases compile time. Including headers you don't need will make compile time longer than it needs to be.

If compile time turns out to be an issue, you can use precompiled headers so that the "group header" only needs to be compiled once. (Though I personally avoid PCHs unless compile time is really slow).

Are there other disadvangtages?


Including lots of headers isn't really "bad" unless the headers themselves are bad (ie: lots of macros/identifiers polluting the namespace).

The only thing apart from compile time that you'd need to be concerned about is circular includes. But if all these headers are part of external libraries that isn't going to be a problem.
Last edited on
Not the thread OP, but...


Including lots of headers isn't really "bad" unless the headers themselves are bad (ie: lots of macros/identifiers polluting the namespace).


What is meant by the italicized statement? How does the namespace become "polluted" by macros/identifiers? Do you just mean basic naming conflicts like two separate variables with the same name and type, or is something more meant by this?

The only thing apart from compile time that you'd need to be concerned about is circular includes. But if all these headers are part of external libraries that isn't going to be a problem.


Aren't circular includes, outside of external libraries, able to be taken care of by preprocessor directives like #ifndef/#define/#endif? Or is that only for multiple includes of the same header?
Do you just mean basic naming conflicts like two separate variables with the same name and type, or is something more meant by this?


Mostly that's what I meant, yes. Other things like using namespace xxx; in header files can also bork up the works.


Aren't circular includes, outside of external libraries, able to be taken care of by preprocessor directives like #ifndef/#define/#endif? Or is that only for multiple includes of the same header?


A circular dependency is where a.h includes b.h and b.h also includes a.h. This can cause "undeclared identifier" errors -- even if you guard with #ifndef because both header files are expecting the other class to be defined... so whichever class gets defined first is going to be missing the other class definition.

But this isn't really a problem with the technique OP is describing... it's more of a problem with just carelessly including headers (usually in other headers). If he's just clumping headers from an external lib into a single header then this isn't really an issue.
Topic archived. No new replies allowed.