1. 程式人生 > 其它 >C/C++ 包管理器 Conan使用指南

C/C++ 包管理器 Conan使用指南

C/C++ 包管理器 Conan使用指南

簡介

類似pip之於Python的包管理器,ConanC/C++的去中心化包管理器,對於CMake有很好的支援

安裝

Conan可以通過Python的包管理器安裝

$ pip install conan

配置

安裝源

Conan是去中心化的,需要配置安裝源來搜尋安裝包,預設已新增conancenter作為安裝源

# 檢視所有安裝源
$ conan remote list
# 新增源
$ conan remote add my-repo http://my-repo.com/xxx
# 刪除源
$ conan remote remove my-repo

GCC ABI 相容配置

安裝Visual Studio的可以忽略此配置

GCC版本大於5.1時可以選擇以下配置來避免使用老舊的ABI

If you are using GCC compiler >= 5.1, Conan will set the compiler.libcxx to the old ABI for backwards compatibility. In the context of this getting started example, this is a bad choice though: Recent gcc versions will compile the example by default with the new ABI and linking will fail without further customization of your cmake configuration. You can avoid this with the following commands:

$ conan profile new default --detect  # Generates default profile detecting GCC and sets old ABI
$ conan profile update settings.compiler.libcxx=libstdc++11 default  # Sets libcxx to C++11 ABI

使用

搜尋包

首先搜尋包

# 從安裝源conancenter搜尋名為zeromq的C/C++的包
# -r 指定搜尋的遠端庫名稱,不加則預設本地倉庫,-r all 搜尋所有遠端庫
$ conan search -r conancenter zeromq
# 檢視包詳細資訊
$ conan inspect zeromq/4.3.2

安裝及CMake配置

本地建立一個檔案conanfile.txt,配置Conan

[requires]
zeromq/4.3.2

[generators]
cmake

即指定安裝包zeromq/4.3.2並且使用CMake構建

然後執行命令安裝依賴並且生成Conan的CMake檔案,用於查詢依賴

$ mkdir build && cd build
$ conan install ..

如果報錯ERROR: Missing prebuilt package,那就換成conan install .. --build missing

然後修改你的CMake檔案,讓其加入對於Conan的支援

cmake_minimum_required(VERSION 3.17)
project(cppproject C)

set(CMAKE_C_STANDARD 99)

# 引入Conan的CMake
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(cppproject main.c)
# 連結Conan的第三方依賴
target_link_libraries(cppproject ${CONAN_LIBS})

完成。

參考