Search code examples
c++header-files

C++ - Batching multiple header files into one


I'm working on a basic game engine, been following the advice to include only what i need. I'm noticing that at places I'm including ton of stuff from certain modules, like Math or Render. And then one is supposed to maintain these long lists regularly so it is really only what one needs, no less no more.

But is it really that big deal to include stuff I don't need? Can't I just include Render.hpp and Math.hpp where I use render and math stuff and be done, instead hunting down all the headers for every little component separately and maintaining a long list of includes? The professional advice is to always only include what I need, but then most libraries I use, they are included either by one include or separately their major modules, but not all their headers one by one.

Does including more than I need really create such a mess and increase compile/parse time so much?


Solution

  • The professional advice is to always only include what I need

    This is an advice, do not blindly apply it, understand why it exists so you don't go into extremes such as overusing object oriented programming and design patterns that led to lot of horrid codebase in lot of companies. Drinking water is good practice but drinking too much lead to death.

    The downside of including more headers is increased compilation time (and namespace pollution in C or if you misused using namespace). This is only an issues on large projects or huge dependencies such as <Windows.h> and their feature exclusion #defines. I would say that importing stuff unrelated to your code is bad practice and may violate logical separation, a backend agnostic game engine will never import D3D12 or Vulkan headers outside parts that are backend specific.

    By what you are saying, it look like having multiple thematic header files with a bunch of includes would make your project more maintainable and less a hassle, the trade-off is probably worth the increased compilation time, especially if you loose more time to figure out the exact header listing and the higher risk of mistakes in maintaining this listing.

    All big game engines actually have a few if not a single "Math.h" with all the regular linear algebra and more they use. Same for their various modules. You already identified Math and Render as modules that may deserve their own header in your question, you may create -internal.h variants for the #includes commonly used inside these modules that should not be publicly exposed to other modules using it.


    So. In C and C++, #include is literally a copy-paste of the included file content. Having a file with only #includes is common to create thematic headers. You should think about what you commonly include and define comprehensive scopes for each header in order to keep your project maintainable.

    What matter ultimately is soundness (see my example about Vulkan/D3D12) and that you spend less time solving issues, waiting for and suffering about your #includes.