fsan

Conan Quick guide

Posted at — Oct 31, 2020

Just enought information to get you started with Conan.

Package managing for C++ is quite difficult, specially while working of several different projects with different dependencies. The objective of conan is to provide some package management for this situation. It is pretty similar in concept to pip and npm but it does also provide some extra functionalities such as generators.

Installing Conan

Installing conan is simple. Only pip install it. I particularly do not like to add stuff to my base pip environment and as I regularly experiment with other tools I usually create environments for this kind of stuff. I used conda to create my environment, but it is totally optional.

conda create -n cpp_env python=3.8

pip install conan

Basic usage

Finding packages

Locally:

conan search <package_name>

Conan Center

conan search <package_name> --remote=<remote_name>

# example
conan search opencv --remote=conan-center

If you wish you may use it’s web interface: https://conan.io/center

To list the package properties:

conan inspect <package_name> --remote=<remote_name>

# example
conan inspect opencv/4.3.0@conan/stable --remote=conan-center

Installing other repositories

Some packages may not be available from Conan source or may have only its binary distributed (or maybe you wish to get the precompiled binaries). You can add other remotes as adding bincrafters below:

conan remote add bincrafters https://api.bintray.com/conan/bincrafters/public-conan

More info at BinCrafters

To list available remotes

conda remote list

Creating package list

You should create a $conanfile.txt$ in your project. It will have similar function as requirements.txt for python or packages.json for node.

[requires]
openssl/1.0.2s
opencv/4.3.0@conan/stable
sdl2/2.0.12@bincrafters/stable

[generators]
cmake

Risking to state the obvious, requires define what packages you want and generators will allow you to create the files for building from cmake, visual studio, premake, make, etc…

An important reference for this is Conan Generators page

There are some useful generators for documenting your application such as markdown that might be worth checking.

Download dependencies and creating configuration files

For organization it is usually nice to have build files inside a separate folder (usually $build$). So if your $conanfile.txt$ is at the root of your project you would run

conan install ..

But if prebuilt binaries are not available you will receive an “BinaryMissing” error. So you would need build the dependencies as:

conan install .. --build=missing

Building the application

You may be using other building systems, but for this TIL I will use cmake because it’s what I have here. For CMake you will need a $CMakeLists.txt$. As we previously added cmake generator, conan will create a $conanbuildinfo.cmake$ so we need to include it in our cmake configuration at the root of our src.

cmake_minimum_required(VERSION 3.10)

project(example_app)
add_definitions("-std=c++14")

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

# add the executable
add_executable(example_app main.cpp)
target_link_libraries(example_app ${CONAN_LIBS})

Now from the build folder

cmake ..
cmake --build .

If everything is fine with your dependencies your application should be built.

comments powered by Disqus