Conan for C++ | CMakeDeps, Profiles, Lockfiles & Private Remotes

Conan for C++ | CMakeDeps, Profiles, Lockfiles & Private Remotes

이 글의 핵심

Conan installs C++ dependencies and generates CMake toolchain files—profiles, lockfiles, and multi-config workflows explained.

What is Conan? why you need it

Problem Scenario: Dependency Hell

Problem: I’m trying to use Boost, fmt, spdlog, and OpenSSL in a C++ project. You must manually download each library, build it, and hardcode the header/library paths into CMakeLists.txt. If team members’ environments are different, the path will be different, causing the build to break, and the entire process must be repeated when updating the version.

Solution: Conan is a C++ package manager, like pip in Python and npm in Node.js. If you specify dependencies in conanfile.txt, Conan automatically downloads, builds, and creates a CMake configuration file. Team members can configure the same environment by simply running conan install.

flowchart LR
    subgraph input["input"]
        conanfile["conanfile.txt\nboost/1.80.0\nfmt/9.1.0"]
    end
    subgraph conan["Conan"]
        install["conan install"]
        download["Download package"]
        build["Build (if needed)"]
        gen["Generate CMake file"]
    end
    subgraph output["output"]
        cmake["conan_toolchain.cmake"]
        deps["CMakeDeps"]
    end
    conanfile --> install
    install --> download
    download --> build
    build --> gen
    gen --> cmake
    gen --> deps

index

  1. Conan installation and basic usage
  2. conanfile.txt vs conanfile.py
  3. CMake integration
  4. Profiles and Settings
  5. Frequently occurring problems and solutions
  6. Production Patterns
  7. Complete example: multi-library project

1. Conan installation and basic use

installation

# Install with pip
pip install conan

# Check version
conan --version

# Conan 2.x recommended

Basic workflow

# 1. Create conanfile.txt
cat > conanfile.txt << 'EOFC'
[requires]
boost/1.80.0
fmt/9.1.0

[generators]
CMakeDeps
CMakeToolchain
EOFC

# 2. Install dependencies
conan install . --output-folder=build --build=missing

#3. CMake build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build .

conanfile.txt structure

[requires]
boost/1.80.0
fmt/9.1.0
spdlog/1.11.0

[generators]
CMakeDeps
CMakeToolchain

[options]
boost:shared=False
fmt:header_only=True

2. conanfile.txt vs conanfile.py

conanfile.txt (simple project)

[requires]
boost/1.80.0
fmt/9.1.0

[generators]
CMakeDeps
CMakeToolchain

conanfile.py (complex project)

from conan import ConanFile
from conan.tools.cmake import cmake_layout

class MyProjectConan(ConanFile):
    name = "myproject"
    version = "1.0"
    settings = "os", "compiler", "build_type", "arch"
    
    def requirements(self):
        self.requires("boost/1.80.0")
        self.requires("fmt/9.1.0")
        
# conditional dependency
        if self.settings.os == "Linux":
            self.requires("openssl/3.0.0")
    
    def configure(self):
# Option settings
        self.options["boost"].shared = False
    
    def layout(self):
        cmake_layout(self)
    
    def generate(self):
# Custom creation logic
        pass

3. CMake integration

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(MyProject)

set(CMAKE_CXX_STANDARD 20)

# Use files generated by Conan
find_package(Boost REQUIRED COMPONENTS filesystem)
find_package(fmt REQUIRED)

add_executable(myapp main.cpp)

target_link_libraries(myapp PRIVATE
    Boost::filesystem
    fmt::fmt
)

Build script

#!/bin/bash

# Install dependencies
conan install . --output-folder=build --build=missing \
    -s build_type=Release \
    -s compiler.cppstd=20

# CMake build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake \
         -DCMAKE_BUILD_TYPE=Release
cmake --build . -j$(nproc)

4. Profiles and Settings

Check profile

# Default profile
conan profile show default

# Profile detection
conan profile detect

Custom Profile

# Create profile
cat > ~/.conan2/profiles/gcc12 << 'EOFP'
[settings]
os=Linux
arch=x86_64
compiler=gcc
compiler.version=12
compiler.libcxx=libstdc++11
compiler.cppstd=20
build_type=Release
EOFP

# Use profile
conan install . --profile=gcc12 --output-folder=build --build=missing

Installation by build type

# Debug
conan install . -s build_type=Debug --output-folder=build/debug --build=missing

# Release
conan install . -s build_type=Release --output-folder=build/release --build=missing

5. Frequently occurring problems and solutions

Issue 1: Package not found

Symptom: ERROR: Package 'boost/1.80.0' not found in local cache.

Cause: The package is not in the local cache and cannot be found remotely.

# Solution 1: Build with --build=missing
conan install . --build=missing

# Solution 2: Add remote repository
conan remote add conancenter https://center.conan.io

# Solution 3: Search for packages
conan search boost --remote=conancenter

Issue 2: Compiler version mismatch

Symptom: ERROR: Missing prebuilt package for 'boost/1.80.0'.

Cause: No prebuilt package matching the profile’s compiler version.

# Solution 1: Build locally with --build=missing
conan install . --build=missing

# Solution 2: Edit profile
conan profile detect --force

# Solution 3: Specify a specific compiler version
conan install . -s compiler.version=11 --build=missing

Issue 3: CMake integration fails

Symptom: find_package(Boost) failed.

Cause: CMAKE_TOOLCHAIN_FILE is not specified.

# ❌ Incorrect use
cmake ..

# ✅ Correct use
cmake .. -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake

Issue 4: Build type mismatch

Symptom: Link error when installed as Debug but built as Release.

Cause: When installing Conan, build_type and CMake build type are different.

# Match
conan install . -s build_type=Release --output-folder=build --build=missing
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake

6. production pattern

Pattern 1: Support for multiple build types

# Debug
conan install . -s build_type=Debug --output-folder=build/debug --build=missing
cmake -B build/debug -DCMAKE_BUILD_TYPE=Debug \
      -DCMAKE_TOOLCHAIN_FILE=build/debug/conan_toolchain.cmake

# Release
conan install . -s build_type=Release --output-folder=build/release --build=missing
cmake -B build/release -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_TOOLCHAIN_FILE=build/release/conan_toolchain.cmake

Pattern 2: Lock version with lock file

# Create lock file
conan lock create conanfile.txt --lockfile-out=conan.lock

# Install with lock file (exact version)
conan install . --lockfile=conan.lock --output-folder=build --build=missing

Pattern 3: Private Package Server

# Add internal company Artifactory
conan remote add company https://artifactory.company.com/conan

# certification
conan remote login company admin -p password

# installation
conan install . --remote=company

Pattern 4: Reproducible builds with Docker

FROM ubuntu:22.04

RUN apt-get update && apt-get install -y python3-pip cmake g++
RUN pip3 install conan

WORKDIR /app
COPY conanfile.txt .
RUN conan install . --output-folder=build --build=missing

COPY . .
RUN cd build && cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake && make

7. Complete example: HTTP server

conanfile.txt

[requires]
boost/1.80.0
fmt/9.1.0
spdlog/1.11.0
openssl/3.0.0

[generators]
CMakeDeps
CMakeToolchain

[options]
boost:shared=False
boost:without_test=True

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(HttpServer VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 20)

find_package(Boost REQUIRED COMPONENTS system thread)
find_package(fmt REQUIRED)
find_package(spdlog REQUIRED)
find_package(OpenSSL REQUIRED)

add_executable(http_server
    src/main.cpp
    src/server.cpp
)

target_link_libraries(http_server PRIVATE
    Boost::system
    Boost::thread
    fmt::fmt
    spdlog::spdlog
    OpenSSL::SSL
)

Build

# Install dependencies
conan install . --output-folder=build --build=missing -s build_type=Release

# CMake build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build . -j$(nproc)

# execution
./http_server

Conan 2.x major changes

ItemConan 1.xConan 2.x
profile~/.conan/profiles~/.conan2/profiles
commandconan install. --install-folder=buildconan install. --output-folder=build
Generatorcmake, cmake_find_packageCMakeDeps, CMakeToolchain
RecipeConanFileConanFile (API change)

organize

conceptDescription
ConanC++ package manager
conanfile.txtSimple dependency statement
conanfile.pyComplex build logic
conan installInstall dependencies and create CMake files
CMakeToolchainCreate CMake toolchain file
CMakeDepsCreate Config file for find_package

Conan automates dependency management for C++ projects, eliminating environment differences between team members and ensuring build reproducibility.


FAQ

Q1: Conan vs vcpkg?

A: Conan is flexible and easy to build a private package server based on Python. vcpkg is created by Microsoft, has good Windows integration, and is simple to use. Choose based on your team environment and preferences.

Q2: conanfile.txt vs conanfile.py?

A: conanfile.txt is suitable for simple projects, conanfile.py is for complex projects that require conditional dependencies and custom build logic.

Q3: What is —build=missing?

A: If no prebuilt package is available, build it locally. It may take a while to install for the first time, but it’s fast afterward thanks to the cache.

Q4: Why do I need a profile?

A: Specify compiler, OS, architecture, build type, etc. The same package is built differently depending on the profile, so unifying the profiles across team members can eliminate differences in environments.

Q5: Where is the Conan cache?

A: Package is cached in ~/.conan2/p/. You can clean it up with conan cache clean.

Q6: What are Conan learning resources?

A:

One-line summary: Conan lets you manage your C++ dependencies automatically. Next, it would be a good idea to read the vcpkg guide.


Good article to read together (internal link)

Here’s another article related to this topic.

  • C++ vcpkg complete guide | Microsoft C++ Package Manager
  • C++ CMake Complete Guide | Cross-platform build·latest CMake 3.28+ features·presets·modules
  • C++ CMake find_package complete guide | External library integration

Practical tips

These are tips that can be applied right away in practice.

Debugging tips

  • If you run into a problem, check the compiler warnings first.
  • Reproduce the problem with a simple test case

Performance Tips

  • Don’t optimize without profiling
  • Set measurable indicators first

Code review tips

  • Check in advance for areas that are frequently pointed out in code reviews.
  • Follow your team’s coding conventions

Practical checklist

This is what you need to check when applying this concept in practice.

Before writing code

  • Is this technique the best way to solve the current problem?
  • Can team members understand and maintain this code?
  • Does it meet the performance requirements?

Writing code

  • Have you resolved all compiler warnings?
  • Have you considered edge cases?
  • Is error handling appropriate?

When reviewing code

  • Is the intent of the code clear?
  • Are there enough test cases?
  • Is it documented?

Use this checklist to reduce mistakes and improve code quality.


Keywords covered in this article (related search terms)

This article will be helpful if you search for C++, conan, package, dependency, tools, build, etc.


  • C++ CMake find_package complete guide | External library integration
  • C++ CMake Targets Complete Guide | Target-based build system
  • C++ CMake Complete Guide | Cross-platform build·latest CMake 3.28+ features·presets·modules
  • C++ vcpkg complete guide | Microsoft C++ Package Manager
  • C++ Benchmarking |