अंतिम अद्यतन:2026-10-12
Conan C++ पैकेज मैनेजर की शुरुआत¶
1. Conan इंस्टॉल करना¶
2. थर्ड-पार्टी लाइब्रेरी इंस्टॉल करना¶
थर्ड-पार्टी लाइब्रेरी इंस्टॉल करते समय हमें आमतौर पर इंस्टॉल की जाने वाली लाइब्रेरी का version पता होना चाहिए, इसके लिए conan एक संबंधित search कमांड उपलब्ध कराता है जो संबंधित लाइब्रेरी के versions की सूची दिखाता है।
अगर सीधे search करेंगे तो error आएगा, conancenter नहीं मिल पाएगा, इसलिए पहला कदम remote सेट करना है।
इसके बाद जब हम ऊपर वाला search कमांड दोबारा चलाते हैं, तो 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.
अब certificate कॉन्फ़िगर करने की ज़रूरत है
आउटपुट:
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/.
इसके बाद, conan search का उपयोग जारी रखने पर हमें यह मिलता है:
[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. एक प्रोजेक्ट लिखना¶
आगे बढ़ते हुए, conan का उपयोग करके एक प्रोजेक्ट लिखते हैं।
प्रोजेक्ट की पृष्ठभूमि: development चरण में हमें unit testing करने की ज़रूरत होती है, उदाहरण के लिए: division में 0 से और गैर-शून्य से भाग देने जैसे boundary cases के assertions की जांच करनी होती है, इसके लिए हमें थर्ड-पार्टी लाइब्रेरी चाहिए, यहाँ gtest का उपयोग किया गया है।
1.कोड लिखें
#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.conanfile.txt लिखें, एक gtest version निर्दिष्ट करें।
3.CMakeLists.txt जोड़ें, इसकी सामग्री बहुत सरल है: इसमें विस्मयादिचक चिह्न वाले हिस्सों पर ध्यान देने की ज़रूरत है।
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.इंस्टॉल और संकलन शुरू करें
- थर्ड-पार्टी लाइब्रेरी इंस्टॉल करें
[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
- संकलन करें
[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
- चलाएँ
[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.
संबंधित विषय¶
- Bazel से C++ प्रोजेक्ट बिल्ड करना | C++那些事
- Bazel और LLDB संयुक्त डिबगिंग | C++那些事
- CMake की शुरुआत और व्यवहार | C++那些事
- C++ Insights कंपाइल-टाइम अवलोकन | C++那些事
- C++ Jupyter Notebook इंटरैक्शन | C++那些事
- C++ string को int में बदलना | C++那些事
- C++ dbg-macro डीबगिंग मैक्रो | C++那些事
- GCC कंपाइलेशन विकल्पों की विस्तृत जानकारी | C++那些事
- GDB से C++ प्रोग्राम डीबग करना | C++那些事
- GDB Python एक्सटेंशन डीबगिंग | C++那些事