Saltar a contenido

Última actualización:2026-10-12

Introducción a Conan, gestor de paquetes de C++

1. Instalación de Conan

pip3 install conan

2. Instalar bibliotecas de terceros

Al instalar bibliotecas de terceros solemos necesitar conocer la versión de la biblioteca instalada; conan ofrece para ello el comando de búsqueda correspondiente, que lista las versiones de la biblioteca.

conan search gtest --remote=conancenter

Si buscas directamente obtendrás un error porque no encuentra conancenter, así que el primer paso es configurar el remote.

conan remote add conancenter https://center.conan.io

A continuación, volvemos a ejecutar el comando de búsqueda anterior y nos encontramos con este 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.

En este punto hay que configurar el certificado

conan config install https://github.com/conan-io/conanclientcert.git

Salida:

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/.

Después, seguimos usando conan search y encontramos:

[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. Escribir un proyecto

A continuación, escribimos un proyecto con conan.

Contexto del proyecto: en la fase de desarrollo necesitamos hacer pruebas unitarias; por ejemplo, para la división hay que comprobar con aserciones los casos límite de dividir entre 0 y entre valores no nulos, para lo cual introducimos una biblioteca de terceros; aquí usamos gtest.

1.Escribir el código

#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.Escribir conanfile.txt, especificando una versión de gtest.

[requires]
gtest/1.13.0

[generators]
cmake

3.Añadir CMakeLists.txt; el contenido es muy sencillo: conviene prestar atención a las partes marcadas con signos de exclamación.

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.Instalar y compilar

  • Instalar la biblioteca de terceros
[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
  • Compilar
[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
  • Ejecutar
[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.

Temas relacionados

Comentarios