The burden of compilation time
Posted on
For the sake of brevity, in this post I will just name "compilation" what would otherwise be named "compilation and linkage". Then the C++ workflow just consists in cyclic "compile and run" iterations. But compilation has nothing to do with the actual problem your are trying to solve with your program. It is just a step required by the C++ language. With no added value whatsoever. A lot of languages don't even need compilation! How many times a day do you compile your code? And especially, how much time does compilation cost you overall? Only a few minutes a day? Good news, you are fine! An hour or more? That would be really worth questionning! C++ programs are prone to require big compilation times. In fact, this is one of the reason that had Google develop the Go language in the first place! The web page linked here above gives clues as to how we can get faster compilation times in C++ projects. The problem in C++ has a variety of factors. One of which is templates. Precisely, their poor integration with the compilation model. There is an aweful lot to say about templates in general. They are very powerful tools for code reusability. In other words, productivity. But they induce such a bloat of the compilation time that it may actually counterweight that advantage! One way to mitigate the bloat is by declaring only the required code in each translation unit (e.g. each .cpp file). The definitions of the template functions or methods can be split up across one or many .inl files (NB: .inl stands for "inline"). Those can be included just like header files. Then, you could include only the required files for the template methods you actually call, and leave the rest not included. This way, you will reduce the charge of your compiler, and keep the .o object files has small as possible. With the benefit of faster linkage time as well. Please note that some developers name those files ".tpp" or ".txx" instead. But it actually does not matter at all to the compiler.