Get Started Easier: Hands-on Tutorial in Conan (C++ Package Management)

KTY
4 min readFeb 4, 2022

--

I need to use external packages in C++, but installing all external packages is such an abysmal pain! Where is my “pip” in C++?

We have convenient package management tools for most of the programming languages. We have associated package management toolkit cargo even for Rust, which is relatively low-level.

One-line style package installation in JavaScript, Python, Go, and Rust

But when it comes to C++, it is painful to go through all the installation procedures for these external packages, which is not beginner-friendly!

A few months ago, I had the opportunity to take care of a newly-launched C++ project. I re-searched for the C++ package management tool and finally found an appropriate one, Conan!

Conan is relatively straightforward to grasp and use (though not in the one-line command fashion). However, it still took some time to comprehend and get familiar with Conan. Hence, I decided to make a hands-by-hands tutorial with a toy (and perhaps boring) example, calling Board API.

Install Conan

You can either download Conan from here or directly via pip install (recommended).

What are we going to build?

Here is our toy example. Feel free to checkout! However, we won’t go into the details. We will name this file main.cpp leave it untouched in the rest of the tutorial!

We have two primary steps and utilize two external packages (cpr and nlohmann_json).

  1. Call Board API to get a random event via cpr
  2. Parse JSON String via nlohmann_json
Screenshot of Board API documentation

Install packages via Conan

Search

To begin, conan search on conancenter (central repository for c/c++ packages). If there are corresponding packages, conan search will return a list of available versions.

Conanfile.txt

conanfile.txt specifies how Conan installs all required dependencies and generates essential information for the subsequent build processes.

Here we tell Conan to install cpr and nlohmann_json and generate information for further CMake procedures.

So on, our working directory contains only two files.

Let’s create a directory build (just for the organization purpose, we will let Conan place all generated files generated here) and run conan install.

We run conan install .. to install packages according to conanfile.txt in the parent folder. Also, all Conan-generated files will be in the build folder. Let’s see what we got!

Build

CMakeLists.txt

Create your CMakeLists.txt as follow in the root folder

  • My project is called main. You can change it as you like.
  • Remember to include conanbuildinfo.cmake, which conan install had prepared.

CMake

Finally, we are ready to execute CMake commands! We get our binary executables in ./build/bin/!

Let’s test our toy C++ program, and it works!

Recap

  • Install external packages via conan install
  • Remember to include conanbuildinfo.cmake in your CMakeLists.txt
  • Change conanfile.txt if you have changed your dependencies and re-run conan install

Disclaimer: I don’t consider myself a professional C++ developer!

I write articles with motivation to help anyone like previous me to get started easier! I hope this article helps.

--

--

KTY
0 Followers

Write articles to make our coding jounery easier