Some Cmake Goodies

Recently I had a discussion where a colleague at work was looking for advice on how to keep 3rd party dependencies for his C/C++ project up to date.

This is actually rather straightforward if you use package managers like vcpkg or conan. Turns out there’s also a pure cmake approach. The cmake function FetchContent is a very generic way to integrate dependencies into your build tool chain. All you need is a link to a git repository, a zip/tar archive or anything similar, and you are good to go.

The example in the official cmake documentation uses google test, but I’ve tried it with Protobuf as well, and it worked flawlessly. What I find very appealing about this solution is that you do not need any additional setup beyond your cmake installation. I could also imagine a scenario where your CI/CD pipeline does your build with different versions of a 3rd party dependency to check whether nothing is going to break in future releases.

Speaking of google test: its official quickstart page lists a rather complete example for setting it up with cmake. It also uses the function gtest_discover_tests which I wasn’t aware of. Its documentation can be found here. This nice little gem adds tests with CTest by querying the compiled test executable for available tests. Very nice!

Notes

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

Mathematician and
Software Engineer

Researcher, Engineer, Tinkerer, Scholar, Philosopher, and Hyrox afficionado