c++compilation

Does splitting your files into .cpp and .h decrease compile time?


I have a big .cpp file with several classes defined and declared in one single file. I was thinking of having a .h file for each class but I am not sure about the advantages.

Is splitting your files pure preference? Would this increase compile time AND/OR general readibility? My compiler gives me a list of all class declarations that I can easily access with one click, hence if the compiler needs more time putting all those files together, I rather keep it in one chunk.

Edit: I am asking splitting into .cpp and .h and not multiple cpp's as this question has been dismissed that there is already an answer.


Solution

  • Having multiple files will not directly decrease the time, however as compilation can be carried out differentially, where only changed code is re-compiled to object files before linking means that if you have each set of classes in their own .cpp and .h files you can potentially save a lot of time. It also has corollary effects such as:

    This is by no means an exhaustive list, just a brief introduction really, and there can be almost as many arguments against it as for, but in general the pros outweigh the cons for me.