CO311: Multiple-file C++ Projects

One of the advantages of the modularity inherent in C++ is the ability to place different parts of a single project into different files. This permits each file to be compiled separately, which can make compilation run much faster as you work along a project.

For instance: you have one single project file, and it takes 2 minutes to compile it. If you have to make any change at all, you’re stuck with a 2 minute wait while it compiles and links.

If you can break that project into two files, and File A takes 45 seconds to compile, while the other one–File B–takes 75 seconds, it still takes 2 minutes to build the entire project. However, if you edit only one of the two files, the editor knows that it doesn’t have to re-compile the other one. If you edit File A, it will take 45 seconds to recompile; if you edit File B, it will take 75 seconds to recompile. Either way, it’s less time than recompiling for 120 seconds!

This functionality makes sharing functions possible too: If you have some versatile function hanging out by itself, then any number of programs can include that function with itself. If you have to change the original function to fix it, all the programs that depend on it will “see” the updated version.

Super Trivial Example

The following set of examples shows off both features:

onefile.cpp — all components in one file, compiles as a single module

2fileMain.cpp — the main part of the program
2fileFunc.cpp — implementation of a single, external function
2fileFunc.h — the header file containing the prototype for 2fileFunc.cpp

3fileMain.cpp — the main part of the program
3fileFunc.cpp — implementation of other functions
3fileFunc.h — the header file containing the prototypes for functions in 3fileFunc.cpp