6 ms·
I really hate the repetition from separating out header and definition files so I've been writing my whole codebase headers only. I feel like this kills my com
by wifijammer 2y ago
I really hate the repetition from separating out header and definition files so I've been writing my whole codebase headers only.
I feel like this kills my compile time but I'm not sure how to fix it. Precompiled headers?
- flohofwoe 2y agoIf you have all your application code in headers you'll get the fastest build times (for full rebuilds at least) by including all headers into a single main.cpp file and build just that, since that way there is no redundant code for the compiler to build at all. Of course the downside is that every tiny code change triggers a full rebuild then, but it's quite likely that the most time is spent in the linker anyway, so maybe worth a try.
- runevault 2y agoI think I've heard this called a Unity build where there's a precompile step that just dumps everything into a single file and compiles that so it doesn't have to re-include everything at different compilation units (when I first heard the term I got confused because it was in a game dev context but had nothing to do with the Unity engine lol).
- flohofwoe 2y agoYes, it's typically called unity or jumbo build, and can also be done with regular source files. Cmake has a feature to do that automatically during the build: https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD.html https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD.htm... ...haven't tinkered with it yet though.
- runevault 2y agoHuh did not know cmake had it built in. Thanks for the information!
- asvitkine 2y agoExcept then there's no parallelism since you're only building one file. Ideally you'd split it into N files to take advantage of multiple cores, but then you have to decide how to split it...
- flohofwoe 2y agoRight. My rule of thumb would be "one implementation file per system", or what would be called a "module" in other languages. So that a moderately complex code base ends up with a about a couple dozen source files to build. And each system should only have a single 'public interface header' to keep the number of cross-system include dependencies low.
- ranger_danger 2y agoccache is one solution, or a script/IDE plugin that will create both the header/definition from a signature you provide?
- pjmlp 2y agoC++20 modules. Currently mostly usable on VC++ vlatest, and clang 17, with clang 18 bringing in support for c++23 import std (VC++ already does it). Sadly GCC is still far behind, not to mention all the other ones still catching up with C++17.