Now that we know how to split code across multiple files, we can use that knowledge to build a project that has multiple programs. This is useful when we want to have one program that uses some code, and a separate program that tests the code.
In this section, we will look at a project that demonstrates building multiple different programs that all share some common code. Our code will be split into these files:
library.cxx: The library code we want to use and to test.
We are using a module, but we could replace library.cxx with library.h and library.cpp. The only necessary change would be to #include "library.h" instead of using import library;.
Note that we do not mention the tests.cpp file, as we do not want to build it into this program. We canβt build both main.cpp and tests.cpp into the same program. Each program can only have one main function. main.cpp defines one and the #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN line in tests.cpp forces that file to automatically create a main function in that file.
Depending on the development environment you are using outside of this book, the details of setting up two programs that build from the same files may vary. You may need to set up two separate βprojectsβ in your development environment or you may be able to set up one βprojectβ with multiple build targets.