Ir para o conteúdo

Última atualização:2026-10-12

Introdução ao gerenciamento de pacotes C++ com Conan

1.Instalação do Conan

pip3 install conan

2.Instalar bibliotecas de terceiros

Ao instalar bibliotecas de terceiros, geralmente precisamos saber a versão da biblioteca instalada; o Conan fornece um comando de busca correspondente, que lista as versões das bibliotecas.

conan search gtest --remote=conancenter

Se você buscar diretamente, ocorrerá um erro informando que o conancenter não foi encontrado; por isso, o primeiro passo é configurar o remote.

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

Em seguida, ao executar novamente o comando de busca acima, obtemos o seguinte erro:

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.

Neste momento, é necessário configurar o certificado

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

Saída:

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

Em seguida, continuando a usar o conan search, 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.Escrever um projeto

A seguir, escreva um projeto usando o Conan.

Contexto do projeto: na fase de desenvolvimento, precisamos realizar testes de unidade; por exemplo, para a divisão é necessário testar asserções de fronteira, como a divisão por 0 e por valores diferentes de zero. Para isso, precisamos introduzir uma biblioteca de terceiros e, neste caso, usaremos o gtest.

1.Escreva o 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.Escreva o conanfile.txt, especificando uma versão do gtest.

[requires]
gtest/1.13.0

[generators]
cmake

3.Adicione o CMakeLists.txt, cujo conteúdo é bem simples: atenção às partes marcadas com pontos de exclamação.

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.Inicie a instalação e a compilação

  • Instalar a biblioteca de terceiros
[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
  • Executar
[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.

Tópicos relacionados

Comentários