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})

完成。

参考

posted @ 2022-05-07 10:08  Santiego  阅读(2785)  评论(0编辑  收藏  举报