This is all the code we need to have a minimal non-trivial working project
structure (MNTWPS, since I heard that software people like acronyms).
Setting up the CMake project
In its easiest working, CMake works by executing scripts contained in
CMakeLists.txt files. Subfolders can have their own CMakeLists.txt, which
can be linked on the root one.
In the following root CMakeLists.txt file we are not doing much else than
informing that the two subdirectories src/ and tests/ are part of the
project.
The only noteworthy detail is that we are including CTestbefore we add
the tests/ subfolder. This will automatically call enable_testing(),
which is responsible for registering the tests and creating a make test
target in the final makefile.
Building the library
Let’s take on building the actual library. This is done with
Here we finally make that all this prolixity pays off. We load Catch2 by
calling find_package() and then link finds the system installation of
mylib. We tag test.cpp as executable of target mytest. The subsequent
calls to target_link_libraries() take care of the -l and -I for the actual
build.
Finally we import the Catch2 Cmake scripts and among those we call
catch_discover_tests(), which parses the source looking for TEST_CASEs and
implicitly calls CMake’s add_test() to register them in Ctest and make test.
Run it
So far we wrote a library consisting of two files and a whopping 3 lines of
code. We are testing it with further 4 lines of code. In order to build the
library and test it we had to write 18 lines of CMake.
Let’s check that at least it works. From the root folder:
mkdir build && cd build
cmake ..
make
make test
ctest
We notice that make takes an awful lot of time to run because of all the
stuff that Ctest creates (9.6M to compile 7 lines of C++!).
Conclusion
This achieves the MNTWPS we were after, and to my knowledge it is probably the
simplest example out there (otherwise there would be no point in writing this).
This is the end of this post but it can be the beginning of properly packaging
our new library, for instance using Conan, which is
decently well documented.