Easy Conan + CMake template for C++ projects

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.

[requires]

    protobuf/3.17.1

    grpc/1.39.1

[generators]

    cmake_find_package

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.

find_program(conanexecutable "conan")

if(NOT conanexecutable)

    message(WARNING "Tool conan is not installed. Check README.md for build instructions without conan.")

else()

    message(STATUS "Found conan. Installing dependencies.")

    if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")

        message(STATUS "Downloading conan.cmake from GitHub - conan-io/cmake-conan: CMake wrapper for conan C and C++ package manager")

        file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/master/conan.cmake"

                      "${CMAKE_BINARY_DIR}/conan.cmake")

    endif()

    include(${CMAKE_BINARY_DIR}/conan.cmake)

    conan_cmake_run(CONANFILE conanfile.txt

                    BASIC_SETUP

                    BUILD missing

                    CONFIGURATION_TYPES "Release")

    set(CMAKE_MODULE_PATH "${CMAKE_BINARY_DIR}" ${CMAKE_MODULE_PATH})

endif()

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.

include(cmake/conan_config.cmake)

find_package(Protobuf REQUIRED)

find_package(gRPC REQUIRED)

#...

target_link_libraries(main

    gRPC::grpc++_unsecure

    )

And that is it. If everything went smoothly, the project can be compiled with

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release

cmake --build build --parallel 12

You can see the whole sample code in grpc-conan repo, here.

Published September 11, 2021By buq2

  • 23
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值