Kyle A. answered 05/05/20
Senior Software Engineer Specializing in Systems Programming
Hi!
Your hunches are not wrong here. C++ programs can take ages to compile (hours or days even). I have personally worked a project where a full recompile took over 4 hours on a fast machine.
C++ inherited the header model from C which is a partial cause of this. Including the same header in multiple translation units causes the same header code to be parsed over and over again. In C this isn't as big of a problem because headers are generally small and contain mostly function and struct declarations.
C++ however introduced templates which are basically required to be implemented in the header file. Now instead of parsing a header file being simple, we need to process a Turing complete language inside our language (see Template Metaprogramming) which can take a huge amount of work and time. And if functions are declared in the header file, now if that file is parsed 100 times, that function needs to be parsed that many times with the majority of that work being a complete waste.
Actually if you code in a very restrained style without templates or using much of the Standard Template Library you will find that your code compiles pretty quickly (well at least quickly for C++...). See Unity builds and forward declarations and other build optimization techniques for more information.
This is far from a complete explanation as many articles and books even have been written on this topic. Unfortunately this is the price we pay for the convenience of the features of C++ over C. As is often the case, there is a trade off. In this case compile time vs features.