20 hours ago, cgrant said:
Source code organization is not enforced by the language. This is an organizational decision. Still doesn't answer your question, but one can only speculate why your compile time is so slow. In my experience I find that heavily templated code usually compiles much slower than the non-templated version. I have no experience with UE4 source code so I don't know if this applies.
Yes is true that the language do not force you to a particular organization scheme, but 99% of all c++ applications are composed of thousands of small .cpp files which all gets compiled to translation units separately. This process is much much slower than compiling just a couple of translation units. Compiling one giant translation unit file including tons of other cpp files directly are much faster than compiling each file separatly.
Its the same as when you upload thousands of small image files to your web storage - its painfully slow, even when you upload 3-4 images at once. But uploading just a single compressed archive containing all image files is a lot quicker.
The only reason why you want to prevent large translation units is because of some size limitations of the compiler itself, but i am not sure about this.
I am pretty confident that you can build applications much much faster when you just have one translation unit for each library/executable.
- Guard all .cpp files with a ifndef block like this:
#include "physics.h"
#ifndef PHYSICS_IMPLEMENTATION
#define PHYSICS_IMPLEMENTATION
// ...
#endif //PHYSICS_IMPLEMENTATION
- In the main translation unit for the executable or library:
// All source files are included directly in this translation unit once + only
// The order is important, if physics for example uses rendering you have to include rendering first.
// If rendering requires physics, you have to add another layer between rendering and physics
#include "rendering.cpp"
#include "physics.cpp"
#include "audio.cpp"
// STB Truetype does not include the implementation automatically, you have to set this constant before including the header file
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
#include "assets.cpp"
// ..
- Setup your IDE/Editor that it will compile the main translation unit only. In visual studio you change the item type for every .cpp file to C/C++ header.
Thats is all you may need to get compilation done much faster. Try it out.
The only downside of this method, you have to keep the order and do not include .cpp files into other .cpp files directly - except for the main translation unit.
And yes, making heavy use of templates also increases compile time drastically. Thats the reason why i use them very rarely - mostly for containers like pools and hash tables or to replace nasty macros.