当前位置: 首页 > 工具软件 > cmake-conan > 使用案例 >

C++包管理 - Conan包管理详细解读和应用

宗政松
2023-12-01

1. conan安装

1.1. 服务器安装

不要python2,用python3 >3.5

先安装pip:sudo apt install python3-pip

再安装conan:pip3 install conan

PATH生效:$ source ~/.profile

  

如果出现:conan: command not found

执行链接:sudo ln -s ~/.local/bin/conan /usr/bin/conan

1.2. WINDOWS本地安装

安装python3版本,注意把环境变量勾上。

设置pip源为内地源【下载更快】:pip3 config set global.index-url Simple Index

【防止跟本地Python2版本冲突,不要用pip,用pip3】

安装conan:cmd下 pip3 install conan

 

2. ubuntu升级本地python版本

2.1. ubuntu16.04升级为python3.8

yang-a@wps-1604:~$ python3 -V Python 3.5.2 yang-a@wps-1604:~$ sudo apt update yang-a@wps-1604:~$ sudo apt install software-properties-common yang-a@wps-1604:~$ sudo add-apt-repository ppa:deadsnakes/ppa yang-a@wps-1604:~$ sudo apt update yang-a@wps-1604:~$ sudo apt install python3.8 yang-a@wps-1604:~$ python3.8 -V Python 3.8.7 将 python 各版本添加到 update-alternatives yang-a@wps-1604:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 yang-a@wps-1604:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 2 yang-a@wps-1604:~$ sudo update-alternatives --config python3   There are 2 choices for the alternative python3 (providing /usr/bin/python3).    Selection Path  Priority Status ------------------------------------------------------------ * 0  /usr/bin/python3.5 2  auto mode  1  /usr/bin/python3.5 2  manual mode  2  /usr/bin/python3.8 1  manual mode   Press <enter> to keep the current choice[*], or type selection number: 2 yang-a@wps-1604:~$ python3 -V Python 3.8.7

2.2. 配置pip

注意:通过apt-get安装python的distutils包时需要带上具体的版本,python3.8-distutils而不是python3-distutils

yang-a@wps-1604:~$ sudo apt-get install python3.8-distutils 自行下载get-pip.py yang-a@wps-1604:~$ sudo python3 get-pip.py -i  https://mirrors.aliyun.com/pypi/simple/ yang-a@wps-1604:~$ pip -V pip 20.3.3 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8) yang-a@wps-1604:~$ pip3 -V pip 20.3.3 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8) 配置阿里云源 yang-a@wps-1604:~$ sudo -H pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/ Writing to /home/yang-a/.config/pip/pip.conf 装个conan试试 yang-a@wps-1604:~$ sudo pip3 install conan Defaulting to user installation because normal site-packages is not writeable Looking in indexes: https://mirrors.aliyun.com/pypi/simple/ Collecting conan   Downloading https://mirrors.aliyun.com/pypi/packages/4a/3d/a9b097670956826c1b8c026b916b795e472c997054c2b172ba1556f416ef/conan-1.32.1.tar.gz (628 kB)      |████████████████████████████████| 628 kB 4.7 MB/s Requirement already satisfied: requests<3.0.0,>=2.8.1 in /usr/lib/python3/dist-packages (from conan) (2.9.1) Requirement already satisfied: urllib3!=1.25.4,!=1.25.5,<1.26 in /usr/lib/python3/dist-packages (from conan) (1.13.1) Requirement already satisfied: six<=1.15.0,>=1.10.0 in /usr/lib/python3/dist-packages (from conan) (1.10.0) Collecting node-semver==0.6.1   Downloading https://mirrors.aliyun.com/pypi/packages/08/51/6cf3a2b18ca35cbe4ad3c7538a7c3dc0cb24e71629fb16e729c137d06432/node_semver-0.6.1-py3-none-any.whl (10 kB) Collecting bottle<0.13,>=0.12.8   Downloading https://mirrors.aliyun.com/pypi/packages/bf/44/aeafdd6ca05a8e1c3f91eeeb272a202d5cb1b3b23730a5ca686a81c48d24/bottle-0.12.19-py3-none-any.whl (89 kB)      |████████████████████████████████| 89 kB 1.5 MB/s Collecting colorama<0.5.0,>=0.3.3 ...

2.3. 其他问题

ModuleNotFoundError: No module named 'gdbm'

解决办法:sudo apt-get install python3.8-gdbm

3. Conan变量

Variable

Description

ANDROID_ABI

Just alias for CMAKE_ANDROID_ARCH_ABI

ANDROID_NDK

Defined when one of ANDROID_NDK_ROOT or ANDROID_NDK_HOME environment variables presented

BUILD_SHARED_LIBS

Only if your recipe has a shared option

CMAKE_ANDROID_ARCH_ABI

Set to a suitable value if cross-building to an Android is detected

CMAKE_BUILD_TYPE

Debug, Release… from self.settings.build_type or build_type attribute only if is_multi_configuration

CMAKE_EXPORT_NO_PACKAGE_REGISTRY

Defined by default to disable the package registry

CMAKE_MODULE_PATH

Set to conanfile.install_folder when using cmake_find_package or cmake_find_package_multi

CMAKE_OSX_ARCHITECTURES

i386 if architecture is x86 in an OSX system

CMAKE_PREFIX_PATH

Set to conanfile.install_folder when using cmake_find_package_multi

CMAKE_SYSTEM_NAME

Set to self.settings.os value if cross-building is detected

CMAKE_SYSROOT

Defined if CONAN_CMAKE_SYSROOT is defined as environment variable

CMAKE_SYSTEM_VERSION

Set to self.settings.os_version value if cross-building is detected

CONAN_CMAKE_CXX_EXTENSIONS

Set to ON or OFF value when GNU extensions for the given C++ standard are enabled

CONAN_CMAKE_CXX_STANDARD

Set to the self.settings.compiler.cppstd value (or self.settings.cppstd for backward compatibility)

CONAN_CMAKE_FIND_ROOT_PATH

Definition set only if same environment variable is declared by user

CONAN_CMAKE_FIND_ROOT_PATH_MODE_INCLUDE

Definition set only if same environment variable is declared by user

CONAN_CMAKE_FIND_ROOT_PATH_MODE_LIBRARY

Definition set only if same environment variable is declared by user

CONAN_CMAKE_FIND_ROOT_PATH_MODE_PROGRAM

Definition set only if same environment variable is declared by user

CONAN_CMAKE_POSITION_INDEPENDENT_CODE

Set when fPIC option exists and True or fPIC exists and False but shared option exists and True

CONAN_CMAKE_SYSTEM_PROCESSOR

Definition set only if same environment variable is declared by user

CONAN_COMPILER

Conan internal variable to check the compiler

CONAN_CXX_FLAGS

Set to -m32 or -m64 values based on the architecture and /MP for MSVS

CONAN_C_FLAGS

Set to -m32 or -m64 values based on the architecture and /MP for MSVS

CONAN_EXPORTED

Defined when CMake is called using Conan CMake helper

CONAN_IN_LOCAL_CACHE

ON if the build runs in local cache, OFF if running in a user folder

CONAN_LIBCXX

Set to self.settings.compiler.libcxx value

CONAN_LINK_RUNTIME

Set to the runtime value from self.settings.compiler.runtime for MSVS

CONAN_SHARED_LINKER_FLAGS

Set to -m32 or -m64 values based on the architecture

CONAN_STD_CXX_FLAG

Set to the flag corresponding to the C++ standard defined in self.settings.compiler.cppstd. Used for CMake < 3.1)

Variable

Description

CMAKE_INSTALL_BINDIR

Set to bin inside the package folder.

CMAKE_INSTALL_DATAROOTDIR

Set to share inside the package folder.

CMAKE_INSTALL_INCLUDEDIR

Set to include inside the package folder.

CMAKE_INSTALL_LIBDIR

Set to lib inside the package folder.

CMAKE_INSTALL_LIBEXECDIR

Set to bin inside the package folder.

CMAKE_INSTALL_OLDINCLUDEDIR

Set to include inside the package folder.

CMAKE_INSTALL_PREFIX

Set to conanfile.package_folder value.

CMAKE_INSTALL_SBINDIR

Set to bin inside the package folder.

CONAN_USER_HOME

conan工作目录

4. conan包名定义

Conan 最主要的功能就是可以管理好预编译好的二进制文件用于后续打包。

l 在 Conan 中,一个包的定义: 包名/版本@用户/渠道

l 同一个包在不同配置下(OS / Architecture / Build Type / Compiler / Compiler Version / compiler.libcxx)构建出的文件将分别保存并支持按需获取

举例:yw_asio/2.4.7@yw/CA9Gyw_asio/2.4.7@yw/N18B32

5. conan常用命令

5.1. 常用命令

其中常用的主要是下面三类:

l Consumer commands - 直译为用户/消费者命令,可以理解为客户端

l Creator commands - 直译为生产者命令,可以理解为服务端

l Package development commands - 打包命令

5.2. 我们一般用到的命令

Consumer

conan install

按照conanfile.txt的配置或者-s等参数项,下载需要的conan包或者相关配置。

conan install可以通过--profile参数指定一个文件来描述包信息(包括OS,compiler,arch等)进行校验;未指定时使用的是默认的profile,其位置在:【Linux:~/.conan/profiles/default,windows:C:\Users\XXX\.conan\profiles\default】

profile各字段支持量特别说明

os

['AIX', 'Android', 'Arduino', 'Emscripten', 'FreeBSD', 'Linux', 'Macos', 'Neutrino', 'SunOS', 'Windows', 'WindowsCE', 'WindowsStore', 'iOS', 'tvOS', 'watchOS']

os_build

['AIX', 'FreeBSD', 'Linux', 'Macos', 'SunOS', 'Windows', 'WindowsStore']

arch

 ['armv4', 'armv4i', 'armv5el', 'armv5hf', 'armv6', 'armv7', 'armv7hf', 'armv7k', 'armv7s', 'armv8', 'armv8.3', 'armv8_32', 'asm.js', 'avr', 'mips', 'mips64', 'ppc32', 'ppc32be', 'ppc64', 'ppc64le', 's390', 's390x', 'sh4le', 'sparc', 'sparcv9',  'wasm', 'x86', 'x86_64']

arch_build

['armv5el', 'armv5hf', 'armv6', 'armv7', 'armv7hf', 'armv7k', 'armv7s', 'armv8', 'armv8.3', 'armv8_32', 'avr', 'mips', 'mips64', 'ppc32', 'ppc32be', 'ppc64', 'ppc64le', 's390', 's390x', 'sh4le', 'sparc', 'sparcv9', 'x86', 'x86_64']

compiler

['Visual Studio', 'apple-clang', 'clang', 'gcc', 'intel', 'qcc', 'sun-cc']

compiler.version

clang - ['10', '11', '3.3', '3.4', '3.5', '3.6', '3.7', '3.8', '3.9', '4.0', '5.0', '6.0', '7.0', '7.1', '8', '9']

compiler.libcxx

['None', 'c++_shared', 'c++_static', 'libc++', 'libstdc++', 'libstdc++11']

build_type

['Debug', 'MinSizeRel', 'None', 'RelWithDebInfo', 'Release']

conan search

查询本地缓存包

conan remove XXX

删除本地XXX包或者带有XXX关键字的包

Creator

conan new

按照模板创建一个默认的confile.py

conan create

按照confile.py的配置在本地conan库创建一个conan包,其中包含了源码的管理,build的管理以及打包的管理

conan export-pkg

导出package到本地conan库,一般用-pf参数指定package文件夹的位置

conan upload

上传本地conan库到远程conan库,一般用-r参数指定远程库地址,否则按默认的远程库

Package

conan package

按照conanfile.py中package函数的要求打包到package文件夹

6. conan远程库

6.1. 服务器创建远程conan库

  Repository Key取一个唯一的名字就行,这个名字就是地址。 

6.2. conan本地配置远程库

类似于git里面的本地分支和远程分支的映射

conan remote add yw-conan http://172.16.1.223:9182/artifactory/api/conan/yw-conan

这句话的意思是:远程yw-conan仓http://172.16.1.223:9182/artifactory/api/conan/yw-conan在本地的名字为yw-conan,需要推送远程仓时,带上 -r yw-conan 参数就可以。

默认的情况下,verify_ssl参数为True,如果设置为False,带上False的参数即可:

conan remote add yw-conan http://172.16.1.223:9182/artifactory/api/conan/yw-conan False

conan远程的配置文件在:~/.conan/remotes.json,这个文件里面有描述。

 

windows默认在:c/Users/XXXXXXXX/.conan/remotes.json

6.3. conan远程库的搜索方式

conan远程的配置文件在:~/.conan/remotes.json

 

6.4. 【注意事项】

conan按照配置文件的顺序进行搜索,所以我们可以:

(1)把我们自己的库放在前面 或者

(2)删除我们不想搜索的库

conan在upload时如果不指定-r的话,默认按配置的第一个来。

If no remote is specified, the first configured remote (by default conan-center, use

'conan remote list' to list the remotes) will be used. 

7. 让conan给普通用户使用

如果conan是root安装的,如何让普通用户使用

这种是权限问题,处理方式:

(1)卸载conan以及对应的依赖库

 

sudo pip3 uninstall XXX

(2)修改权限:

umask 022

(3)重新安装conan:

sudo pip3 install conan

 类似资料: