Cmake, Conan, and GTest Example
A minimal example of a project that uses cmake, conan, and gtest. I make use of the very handy cmake-conan wrapper. This setup also works seamlessly in CLion
Source Code: https://github.com/jroddev/basic-conan-cmake-gtest-project
This sample project is what I would consider a minimal example of a project that uses cmake, conan, and gtest. I make use of the very handy cmake-conan wrapper. This setup also works seamlessly in CLion (using cmake directly, not the conan plugin).
First thing you'll probably notice is there are quite a few cmake files.
- ./CMakeList.txt: top-level cmake file, includes the others
- ./cmake/conan.cmake: Setup cmake-conan wrapper
- ./app/CMakeList.txt: Handles building of the app module
- ./app/test/CMakeList.txt: Sets up test executable and adds it to CTest
cmake-conan allows you to configure conan directly in cmake instead of requiring a conanfile.txt. However in order to get this working I would have needed to move code out of conan.cmake and into the root CMakeList.txt. Because I wanted to keep the cmake-conan details hidden I decided to move all the cmake code into the conan.cmake file and to have a conanfile.txt at the root.
Top Level CMakeList.txt
The job of this file is some basic boiler plate and then to include other cmake files. At the end I include the 'app' module using add_subdirectory.
CMake Conan Helper File
Most of this is copy and pasted from the cmake-conan README.md. One modification I made was to opt for a conanfile.txt file at the project root directory.
This is the file most likely to grow with the project. I find, include, and link the spdlog library. I found spdlog_INCLUDE_DIR and spdlog::spdlog by looking in the cmake generated Findspdlog.cmake file. The cmake-conan wrapper will create a Find<dep>.cmake file for each dependency. You can use these files to see what variables are set and what targets are exported.
App Test Module
This file creates, compiles and links the test executable. add_test is used to register this test suite with CTest.
Build and Test
If you're using cmake you can just run the executable and tests from the IDE.
If you're building from the commandline the the .github/workflows/cmake.yml pipeline has a working example. Here is a cleaned, simpler version: