Last updated:2026-10-12
Getting Started with the Conan C++ Package Manager¶
1.Installing Conan¶
2.Installing Third-Party Libraries¶
When installing third-party libraries, we usually need to know the version of the library to install, so Conan provides a corresponding search command that lists the available versions of a library.
Searching directly will fail because conancenter cannot be found, so the first step is to set up the remote.
Then, when we retry the search command above, we get an error:
ERROR: HTTPSConnectionPool(host='center.conan.io', port=443): Max retries exceeded with url: /v1/ping (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:877)'),))
Unable to connect to conancenter=https://center.conan.io
1. Make sure the remote is reachable or,
2. Disable it by using conan remote disable,
Then try again.
At this point, we need to configure certificates.
Output:
Trying to clone repo: https://github.com/conan-io/conanclientcert.git
Repo cloned!
Copying file LICENSE to /home/light/.conan/.
Copying file cacert.pem to /home/light/.conan/.
Then, continuing with conan search, we find:
[light@i-fu3d01tc build]$ conan search gtest --remote=conancenter
Existing package recipes:
gtest/cci.20210126
gtest/1.8.1
gtest/1.10.0
gtest/1.11.0
gtest/1.12.1
gtest/1.13.0
gtest/1.14.0
3.Writing a Project¶
Next, let's write a project using Conan.
Project background: during development we need unit tests — for example, for division we need assertions covering boundary cases such as dividing by 0 versus non-zero — so we need to pull in a third-party library; here we use gtest.
1.Write the code
#include <gtest/gtest.h>
double divide(double a, double b) { return b == 0 ? 0 : a / b; }
TEST(Div, NonZero) {
double expected = 1.5;
double actual = divide(3, 2);
ASSERT_EQ(expected, actual);
}
TEST(Div, Zero) {
double expected = 0;
double actual = divide(3, 0);
ASSERT_EQ(expected, actual);
}
2.Write conanfile.txt, specifying a gtest version.
3.Add CMakeLists.txt. The content is very simple — pay attention to the exclamation-mark parts.
cmake_minimum_required(VERSION 3.16)
project(MultiplyTest LANGUAGES CXX)
enable_testing()
// !!!
find_package(GTest REQUIRED)
add_executable(div_test div_test.cpp)
// !!!
target_link_libraries(div_test
PRIVATE
GTest::GTest)
add_test(div_gtest div_test)
4.Install and build
- Install the third-party library
[gpadmin@i-fu3d01tc build]$ conan install ../
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=10
os=Linux
os_build=Linux
[options]
[build_requires]
[env]
gtest/1.13.0: Not found in local cache, looking in remotes...
gtest/1.13.0: Trying with 'conan-public'...
gtest/1.13.0: Trying with 'conancenter'...
Downloading conanmanifest.txt completed [0.10k]
Downloading conanfile.py completed [8.27k]
Downloading conan_export.tgz completed [0.32k]
Decompressing conan_export.tgz completed [0.00k]
gtest/1.13.0: Downloaded recipe revision 8a0bc5b3e159ed45de97260c2bff65b5
conanfile.txt: Installing package
Requirements
gtest/1.13.0 from 'conancenter' - Downloaded
Packages
gtest/1.13.0:e019a06362b932ca5d1b082b6c112aa150c88de4 - Download
Installing (downloading, building) binaries...
gtest/1.13.0: Retrieving package e019a06362b932ca5d1b082b6c112aa150c88de4 from remote 'conancenter'
Downloading conanmanifest.txt completed [3.07k]
Downloading conaninfo.txt completed [0.60k]
Downloading conan_package.tgz completed [487.88k]
Decompressing conan_package.tgz completed [0.00k]
gtest/1.13.0: Package installed e019a06362b932ca5d1b082b6c112aa150c88de4
gtest/1.13.0: Downloaded package revision 81bd558bf2388a3aee4ee13b8a902a45
conanfile.txt: Generator txt created conanbuildinfo.txt
conanfile.txt: Generator cmake created conanbuildinfo.cmake
conanfile.txt: Aggregating env generators
conanfile.txt: Generated conaninfo.txt
conanfile.txt: Generated graphinfo
- Build
[light@i-fu3d01tc build]$ cmake ..
-- The CXX compiler identification is GNU 10.2.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /opt/rh/devtoolset-10/root/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found GTest: /usr/lib64/libgtest.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/light/cmake-google-tests/conan/build
[light@i-fu3d01tc build]$ make
Scanning dependencies of target div_test
[ 50%] Building CXX object CMakeFiles/div_test.dir/div_test.cpp.o
[100%] Linking CXX executable div_test
[100%] Built target div_test
- Run
[light@i-fu3d01tc build]$ ./div_test
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from Div
[ RUN ] Div.NonZero
[ OK ] Div.NonZero (0 ms)
[ RUN ] Div.Zero
[ OK ] Div.Zero (0 ms)
[----------] 2 tests from Div (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[ PASSED ] 2 tests.
Related Topics¶
- Building C++ Projects with Bazel | C++那些事
- Debugging Bazel Builds with LLDB | C++那些事
- CMake Essentials in Practice | C++那些事
- C++ Insights Compile-Time Observation | C++那些事
- Interactive C++ in Jupyter Notebook | C++那些事
- C++ string to int | C++那些事
- The C++ dbg-macro Debugging Macro | C++那些事
- GCC Compilation Flags Explained | C++那些事
- Debugging C++ Programs with GDB | C++那些事
- GDB Python Extension Debugging | C++那些事