Single Unit Optimization
Just to complement on @Jason's answer, I'd like to post another technique to avoid the limitation that arises when splitting files.
It's called Single Unit Optimization:
The Single Compilation Unit technique uses pre-processor directives to "glue" different translation units together at compile time rather than at link time. This reduces the overall build time, due to eliminating the duplication, but increases the incremental build time (the time required after making a change to any single source file that is included in the Single Compilation Unit), due to requiring a full rebuild of the entire unit if any single input file changes.
The whole project, even when split in files, can be optimized as if all parts of the program were visible to the compiler at once, without requiring the user merges the files back again.
How to apply it?
Usually, the project would contain a file with a main and will include all header files of each split file:
main.c
#include "sub-program-1.h"
#include "sub-program-2.h"
...
#include "sub-program-n.h"
//rest of code
where each of those .h
files correspond to its respective .c
which is compiled on its own (possibly through a makefile).
In order to apply SCU, we remove the include I've mentioned above and instead create a new file (let's call it SCU.c
). This would be the following.
SCU.c
#include "sub-program-1.c"
#include "sub-program-2.c"
...
#include "sub-program-3.c"
#include "main.c"
//no more code in this file
And to compile the whole project, we just compile SCU.c