C++ Package Management With Conan: Introduction

Install

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

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

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.

$ 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
$ mkdir build
$ cd build
$ conan install ..
$ cmake .. -G Ninja
$ ninja
$ cd bin
$ ./testConan
Opened database successfully

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store