C++ Package Management With Conan: Introduction

Ilyas Hamadouche
4 min readOct 12, 2019

--

Managing C++ dependencies can be a tough task. So often, it’s up to the developer to care about downloading source codes, integrating ready-to-use binaries, building, installing, and linking libraries. In this article, I introduce you to Conan, the C/C++ open-source package manager, that does these for you with excellence.

Conan is a free open-source (MIT) package manager developed for C and C++. It is similar to NuGet and npm for .NET and JavaScript respectively. It can run in any operating system that supports Python. This includes Linux, Mac, as well as Windows.

Because there is no standard package manager for C++ yet, Conan is trying to be the one. The tool recently has got more attention from the community and gained trust from big companies such as Huawei, Keysight, and Mercedes-Benz [1].

In this story, I will show you first how to setup Conan. Then, I will go through an example to demonstrate how the tool works in practice.

Install

The common and recommended way to install Conan is using pip:

$ pip3 install conan

The following choices are also valid:

  • Arch (yay): yay -S conan
  • Mac (HomeBrew): brew install conan
  • Build the tool from the source code. See this link for more instructions.

Once the installation is done, you can verify that Conan is working fine by typing:

$ conan --help

Note: In some operating systems, a reboot is required after the installation.

Conan in Action

To see how Conan can be used to fetch and build packages, let’s have a simple C++ project that depends on a third-party library. The following source code, for instance, depends on SQLite. It tries to open an SQLite database called “test.db”. In case it doesn’t find it, it will create a new database file with that name. If the code cannot open or create the file, It prints out the SQLite error message.

To build the source code, we need to use Conan to get the SQLite package for us. We need first to know the name and version of the package we would like to use. The following command will display all the sqlite3 packages available on the remote server conan-center:

$ conan search sqlite3* --remote=conan-center
Existing package recipes:
sqlite3/3.14.1@bincrafters/stable
sqlite3/3.20.1@bincrafters/stable
sqlite3/3.21.0@bincrafters/stable
sqlite3/3.25.3@bincrafters/stable
sqlite3/3.26.0@bincrafters/stable
sqlite3/3.27.1@bincrafters/stable
sqlite3/3.27.2@bincrafters/stable
sqlite3/3.28.0@bincrafters/stable
sqlite3/3.29.0
sqlite3/3.29.0@bincrafters/stable
sqlite3/3.30.1
sqlite3/3.31.0
sqlite3/3.31.1
sqlite3/3.32.1
sqlite3/3.32.2
sqlite3/3.32.3

Let’s select the newest version 3.32.3. Next, we need to create a conanfile.txt file and include the full name of the package. We need also to specify a generator. We can select CMake. The conanfile.txt file should look like the following:

The last file we need to create is CMakeLists.txt. The example below has the necessary settings to run our project.

Now that we have our project ready, the next step is to install and build the dependency library SQLite. To do so, we need to create a new directory named build, then run the Conan install command. This is accomplished by typing the following:

$ mkdir build
$ cd build
$ conan install ..

Next, let’s build our source code. I would like to use the build system Ninja (Learn more about Ninja in this story). To build, type these commands:

$ cmake .. -G Ninja
$ ninja

An executable should be generated in the folder build/bin. You can run it to test the program. Type:

$ cd bin
$ ./testConan
Opened database successfully

You should be able to see that program prints the correct message and a new database file was created in the bin folder with the name “test.db”.

Useful Commands

  • Build all the missing packages from sources if prebuild ones are not available on your machine: conan install .. -b missing
  • Inspect your current project’s dependencies:conan info ..
  • Generate a dependencies graph for the current project: conan info ..-- graph=graph.html

References

Liked the story?

Give Conan a try and leave me feedback. Don’t forget to put 👏 and share this with your friends ;)

About me

I am Ilyas Hamadouche, a Senior Software Engineer at Elektrobit Automotive. I am interested in automotive software, embedded systems, and computer vision. Follow me on Twitter and LinkedIn.

--

--