Skip to main content

Section 6.1 Projects with Multiple Files

As we build up a collection of functions, and tests for those functions, we may want or need to break our code into multiple files. Splitting code up into multiple files can makes it easier to manage, quicker to recompile, and easier to reuse code across multiple related programs.
In the next few sections, we will explore how to split a very simple program into multiple files. Once we learn how the process works, we will use those ideas to create a more realistic project that has some functions in a code library, a unit test program that tests those functions, and a β€œreal” program that depends on the functions.
Recall that the compiler is the tool that turns the C++ code we write into executable machine code. Technically, this happens in two phases. First, our .cpp file is compiled on its own into an object file (compiled code). Then, that code is linked with other code to form the final executable.
Normally we don’t see or think about the two steps. Unless we explicitly tell the compiler not to, it will always both compile our code and then link it with code from the standard library (which handles the messy details of actually starting up a program and then running the main function), as well as any the code from any other libraries like <cmath> that we have included.
Executing this compiler command builds main.cpp and then links that code into an executable program:
$ g++ main.cpp -o program.exe

Note 6.1.1.

Recall that -o says that the following name is what to use for the output file. The file name(s) before that are what to compile. So this recipe says β€œcompile main.cpp into program.exe”.
Figure 6.1.1. Building a single file compiles it and links it with library code
We can tell the compiler to build multiple code files into a single program, as in this command:
$ g++ main.cpp library.cpp -o program.exe
When we do so, the files (main.cpp and library.cpp in this case) are each independently compiled. After that is done, their code is linked together with any needed code from the standard library and any other included libraries:
Figure 6.1.2. Compiling multiple files compile each one, then links them and standard library code together
The critical detail here is that although we have said β€œbuild main.cpp and library.cpp into program.exe”, the two files are each first compiled on their own into object files. When the compiler is working on main.cpp, it must have ALL the information it needs to compile that fileβ€”it will have no awareness of the other files that are also to be compiled. This model is known as separate compilation

Checkpoint 6.1.1.

Checkpoint 6.1.2.

Which is the true statement?
  • Each file is compiled separately into an object file.
  • Each object file is separately linked into an executable.

Checkpoint 6.1.3.

Complete the compiler recipe to build a program called executable.exe using other.cpp and program.cpp. (Place the name of the output at the end of the command.) You will not use all the blocks.
You have attempted of activities on this page.