Conan is bit confusing. It seems to give you a lot of freedom to make mistakes on creating and configuring your project. I want show how Conan and CMake have worked really well for me on my projects.
First we want to define some required packages for the project and generators which help us setup the build system. We can do this by creating conanfile.txt
in the project root.
|
Now we need to do some CMake configuration. We want to use Conan, but also give freedom for the user of the project to choose if they want to handle packages themselves. So we start by creating a CMake file which checks if user has Conan installed.
|
If Conan is found, the script downloads newest version of conan.cmake file from github which helps running Conan directly from the CMake. After including the file, we then execute conan_cmake_run
for which we can set some configuration options such as file from which the Conan requirements, options and generators are read, if we want to build missing packages, and all build we want to use with multi build systems (such as Visual Studio). Finally we include build directory to CMAKE_MODULE_PATH
such that CMakes FIND_PACKAGE
can find FindXXX.cmake
scripts which are generated by cmake_find_package
Conan generator.
Finally in the main CMakeLists.txt
file we include the above script and try to find the required packages for the project and later use the found targets.
|
And that is it. If everything went smoothly, the project can be compiled with
|
You can see the whole sample code in grpc-conan repo, here.
Published September 11, 2021By buq2