Conan C++ Package Manager: Complete Guide with Examples
1. What Is Conan and Why You Need It
Conan is a decentralized, open-source package manager designed specifically for C++. It solves one of the most painful problems in the C++ ecosystem: dependency management. Unlike other languages (Python with pip, JavaScript with npm, Rust with Cargo), C++ long lacked a standard tool for automatically downloading, building, and integrating third-party libraries. Developers had to manually download source code, configure include paths, link libraries, and painfully resolve version conflicts. Conan automates this entire process.
Conan supports all major operating systems (Windows, Linux, macOS) and compilers (GCC, Clang, MSVC, MinGW). It works with various build systems: CMake, MSBuild, Makefiles, SCons, Bazel, and others. Packages in Conan can be distributed as source code (Conan will build them for your platform) or as pre-built binaries. This saves hours of time, especially in large projects with dozens of dependencies.
A key feature of Conan is its profile model. You can define a profile for each platform/compiler combination (e.g., "clang-14-release-x86_64" or "msvc-2022-debug-x86"). Conan automatically selects or builds packages compatible with your profile. This makes it an indispensable tool for cross-platform development and CI/CD pipelines.
2. Installation
Conan is written in Python and installed via pip. It's recommended to install it in a virtual environment (venv) to avoid conflicts with system Python packages.
# Install via pip (recommended in a virtual environment)
python -m pip install conan
# Verify installation
conan --version
# Upgrade to the latest version
python -m pip install --upgrade conan
After installation, it's recommended to configure the default remote repository — ConanCenter. It's already added by default, but you can verify with:
conan remote list
If ConanCenter is missing, add it:
conan remote add conancenter https://center.conan.io
3. Quick Start — Minimal Working Example
Let's create a simple project that uses the fmtlib library (string formatting) and zlib (data compression).
Step 1: Project Structure
mkdir my_conan_project
cd my_conan_project
mkdir src
Step 2: conanfile.txt
[requires]
fmt/10.2.1
zlib/1.3.1
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
Step 3: CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(MyConanProject CXX)
find_package(fmt REQUIRED)
find_package(ZLIB REQUIRED)
add_executable(my_app src/main.cpp)
target_link_libraries(my_app PRIVATE fmt::fmt ZLIB::ZLIB)
Step 4: src/main.cpp
#include <fmt/core.h>
#include <zlib.h>
#include <iostream>
int main() {
// Using fmt
fmt::print("Hello from Conan! Version: {}\n", FMT_VERSION);
// Using zlib
std::cout << "zlib version: " << zlibVersion() << std::endl;
// Simple compression example
const char* data = "Hello, Conan! This string will be compressed.";
uLong source_len = strlen(data) + 1;
uLong dest_len = compressBound(source_len);
char* compressed = new char[dest_len];
int ret = compress(reinterpret_cast(compressed), &dest_len,
reinterpret_cast(data), source_len);
if (ret == Z_OK) {
fmt::print("Compression successful! Original: {} bytes -> Comp Also in library
Redis Client for C++: Connection, Installation, and Usage Examples
Rate Limiting in C# (.NET): Complete Guide to System.Threading.RateLimiting Library
Monitoring C# (.NET) Applications with AppMetrics
Database Migrations in C# (.NET): Entity Framework, FluentMigrator, and Other Approaches
Authorization and Authentication in Java: Spring Security and JWT