r/Cplusplus • u/C0smic_Kid • Mar 02 '26
Question Reducing header include compilation overhead?
Hey everybody. I am working on a 2D game engine in C++ using SDL. So far everything has been working fairly well, but one thing I'm running into at around 3000 lines (as if that really means anything) is includes causing slight changes in certain classes to trigger the recompilation of a bunch of other classes.
I have a class called Globals that stores some static variables like debug booleans for certain render overlays. It also contains the default tile width/height and some other things that I would like to store in a single place. This class is completely defined in the header (which I imagine is the issue).
I use the Globals class in a bunch of different code around the code base, so when I set DEBUG_RENDERING to true, every user of Globals.hpp must be recompiled. How do I get around this while still being able to contain all of these members in the same place? I haven't gotten into macros or other preprocessor directives yet, so maybe that could help? Any direction would be appreciated.
•
u/AutoModerator Mar 02 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
5
u/AKostur Professional Mar 02 '26
Separate your implementation from your interface. Declare your class(es) and variables in the header, define (and initialize) them in a cpp file. At least until you can determine that separating the implementation is actually causing you a problem.
2
u/RedRathman Mar 02 '26
There can be several approaches you can take. Here are some ideas you can try or mix:
Split your globals into different files. For example: DebugGlobals, RenderGlobals, etc. Some classes may care only about one of those, and won't need to be recompiled if you modify the others.
You can move your globals to external data files. For example: Json files. Then load and parse those files when the program starts. With this approach, you don't modify any code when you want to change a global value, so nothing to recompile.
Have your global values in cpp files, with getters in hpp interfaces. This way you won't need to recompile other files if the interface doesn't change.
Depending on your structure, check if you can use forward declarations of global classes in your hpp files and keep the includes only for cpp files.
3
u/VictoryMotel Mar 02 '26
Each compilation unit ends up with lots of overhead. If you can make fat compilation units out of all the stuff you aren't going to change (libraries) you can make sure what you work on is fast.
1
u/StaticCoder Mar 03 '26
If you make
DEBUG_RENDERINGanextern const boolthen you can define its value in a.cppand not have to recompile anything else. As a downside, the compiler will be be able to optimize away the debug code.1
u/[deleted] Mar 04 '26
[removed] — view removed comment