简明conan教程

本文详细介绍了Conan,一个用于C++项目的依赖管理工具,包括其起源、作用、在Ubuntu和Windows上的安装步骤,以及各种命令的解释和软件包开发流程。重点展示了如何创建和管理Conan软件包,以及配置和使用它们的过程。
摘要由CSDN通过智能技术生成

1 关于conan

Conan是一个C/C++的依赖管理工具。

1.1 来源

Conan最初是由JFrog公司开发的,该公司在软件仓库管理领域有着丰富的经验。Conan的目标是解决C/C++项目中的依赖管理问题,这在C/C++开发中是一个非常复杂和常见的问题。Conan允许开发者定义、发布、分享和安装C/C++库的依赖关系,使得在不同项目和平台上管理和复用代码变得更加容易。

1.2 作用

Conan的主要作用是管理C/C++项目的依赖关系。以下是Conan的主要功能和作用:

依赖定义:Conan允许开发者在conanfile.py或conanfile.txt中定义项目所需的依赖库,包括库的名称、版本、构建选项等信息。

依赖安装:通过Conan,开发者可以轻松地安装项目所需的依赖库,Conan会自动解析并下载相应的库文件。

跨平台支持:Conan支持多种操作系统和编译器,可以方便地在不同的平台上管理和安装依赖。

包管理和分享:Conan允许开发者将自己的库打包成Conan包并分享到Conan中央仓库或其他仓库,其他开发者可以轻松地安装和使用这些库。

构建和集成:Conan支持自定义构建选项和生成不同格式的构建文件(如CMake、Meson等),以便于与现有的构建系统集成。

版本管理:Conan允许开发者明确指定依赖库的版本,确保项目的稳定性和可复用性。

本地缓存和缓存策略:Conan会缓存下载的依赖库,提高后续的安装速度,并支持自定义的缓存策略。

2 安装conan

2.1 ubuntu

2.1.1 安装python3

sudo apt-get update

sudo apt-get install python3

验证python是否安装成功

$ python3 -V

2.1.2 安装conan

最新的conan是2.x版本的,而我们目前使用的是1.xx的conan。

pip install conan=1.xx

2.2 Windows

2.2.1 安装python3

去python官网下载相对比较新的python3软件,然后双击安装即可。

2.2.2 安装conan

最新的conan是2.x版本的,而我们目前使用的是1.xx的conan。

pip install conan=1.xx

2.3 验证conan是否成功

以Windows为例:

PS C:\Users\test> conan
Consumer commands
  install    Installs the requirements specified in a recipe (conanfile.py or conanfile.txt).
  config     Manages Conan configuration.
  get        Gets a file or list a directory of a given reference or package.
  info       Gets information about the dependency graph of a recipe.
  search     Searches package recipes and binaries in the local cache or a remote. Unless a
             remote is specified only the local cache is searched.
Creator commands
  new        Creates a new package recipe template with a 'conanfile.py' and optionally,
             'test_package' testing files.
  create     Builds a binary package for a recipe (conanfile.py).
  upload     Uploads a recipe and binary packages to a remote.
  export     Copies the recipe (conanfile.py & associated files) to your local cache.
  export-pkg Exports a recipe, then creates a package from local source and build folders.
  test       Tests a package consuming it from a conanfile.py with a test() method.
Package development commands
  source     Calls your local conanfile.py 'source()' method.
  build      Calls your local conanfile.py 'build()' method.
  package    Calls your local conanfile.py 'package()' method.
  editable   Manages editable packages (packages that reside in the user workspace, but are
             consumed as if they were in the cache).
  workspace  Manages a workspace (a set of packages consumed from the user workspace that
             belongs to the same project).
Misc commands
  profile    Lists profiles in the '.conan/profiles' folder, or shows profile details.
  remote     Manages the remote list and the package recipes associated with a remote.
  user       Authenticates against a remote with user/pass, caching the auth token.
  imports    Calls your local conanfile.py or conanfile.txt 'imports' method.
  copy       Copies conan recipes and packages to another user/channel.
  remove     Removes packages or binaries matching pattern from local cache or remote.
  alias      Creates and exports an 'alias package recipe'.
  download   Downloads recipe and binaries to the local cache, without using settings.
  inspect    Displays conanfile attributes, like name, version, and options. Works locally,
             in local cache and remote.
  help       Shows help for a specific command.
  lock       Generates and manipulates lock files.
  frogarian  Conan The Frogarian

Conan commands. Type "conan <command> -h" for help
PS C:\Users\test>

2.4 命令解释

这是Conan包管理器的命令列表及其详细解释:

2.4.1 Consumer commands

这些命令主要用于消费者(即使用Conan来获取和使用依赖项的开发者)。

  • install

描述:安装在 conanfile.py 或 conanfile.txt 中指定的依赖项。
用法:conan install .conan install path/to/conanfile.py

  • config

描述:管理Conan的配置。
用法:conan config get, conan config set, conan config rm 等。

  • get

描述:获取给定引用或包的文件或列出目录。
用法:conan get package/version@user/channel 或 conan get reference

  • info

描述:获取有关依赖图的信息。
用法:conan info . 或 conan info path/to/conanfile.py

  • search

描述:在本地缓存或远程搜索包和二进制包。
用法:conan search package/version@user/channel 或 conan search package

2.4.2 Creator commands

这些命令主要用于创建者(即创建和发布Conan包的开发者)。

  • new

描述:创建一个新的包模板,包括 conanfile.py 和可选的 test_package 测试文件。
用法:conan new Hello/0.1 -t -s(-t 和 -s 是可选参数)

  • create

描述:为一个 conanfile.py 构建二进制包。
用法:conan create . user/channel

  • upload

描述:将包和二进制包上传到远程。
用法:conan upload Hello/0.1@user/channel -r=my_remote

  • export

描述:将包(conanfile.py 和相关文件)复制到本地缓存。
用法:conan export . user/channel

  • export-pkg

描述:导出一个包,然后从本地源代码和构建文件创建一个包。
用法:conan export-pkg . user/channel

  • test

描述:使用 conanfile.py 中的 test() 方法测试一个包。
用法:conan test . user/channel

2.4.3 Package development commands

这些命令主要用于包开发。

  • source

描述:调用你的本地 conanfile.py 的 source() 方法。
用法:conan source .

  • build

描述:调用你的本地 conanfile.py 的 build() 方法。
用法:conan build .

  • package

描述:调用你的本地 conanfile.py 的 package() 方法。
用法:conan package .

  • editable

描述:管理可编辑的包(位于用户工作区的包,但被消费如同它们在缓存中)。
用法:conan editable add . 或 conan editable remove .

  • workspace

描述:管理工作空间(从用户工作空间消费的一组包,属于同一个项目)。
用法:conan workspace install .

2.4.4 Misc commands

其他命令。

  • profile

描述:列出 .conan/profiles 文件夹中的配置文件,或显示配置文件详情。
用法:conan profile list 或 conan profile show default

  • remote

描述:管理远程列表和与远程相关的包配方。
用法:conan remote add my_remote https://my_url 或 conan remote list

  • user

描述:使用用户/密码对远程进行身份验证,缓存认证令牌。
用法:conan user -r=my_remote

  • imports

描述:调用你的本地 conanfile.py 或 conanfile.txt 的 imports 方法。
用法:conan imports .

  • copy

描述:将Conan配方和包复制到另一个用户/频道。
用法:conan copy Hello/0.1@user/channel other_user/other_channel

  • remove

描述:从本地缓存或远程删除与模式匹配的包或二进制文件。
用法:conan remove Hello* -f

  • alias

描述:创建并导出一个“别名包配方”。
用法:conan alias Hello/0.1@user/channel Hello/latest@user/channel

  • download

描述:下载配方和二进制文件到本地缓存,不使用设置。
用法:conan download Hello/0.1@user/channel

  • inspect

描述:显示Conan配方属性,如名称、版本和选项。可以在本地、本地缓存和远程工作。
用法:conan inspect Hello/0.1@user/channel

  • help

描述:显示特定命令的帮助。
用法:conan help install

  • lock

描述:生成和操作锁定文件。
用法:conan lock create conanfile.txt, conan lock build conan.lock

  • frogarian

描述:这似乎是一个特殊的命令

3 创建conan软件包

参考文档位置:Getting started

3.1 conan new

使用 conan new 命令将为我们创建一个 "Hello World "C++ 库示例项目:

$ mkdir hellopkg && cd hellopkg
$ conan new hello/0.1 --template=cmake_lib
File saved: conanfile.py
File saved: CMakeLists.txt
File saved: src/hello.cpp
File saved: src/hello.h
File saved: test_package/conanfile.py
File saved: test_package/CMakeLists.txt
File saved: test_package/src/example.cpp

生成文件的简单介绍:

  • conanfile.py:在根文件夹中有一个 conanfile.py,它是主要的配方文件,负责定义软件包的构建和使用方式。
  • CMakeLists.txt:一个简单的通用 CMakeLists.txt,其中没有任何关于conan的特定内容。
  • src:src 文件夹,其中包含简单的 C++"hello "库。
from conans import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout

class HelloConan(ConanFile):
    name = "hello"
    version = "0.1"

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": False, "fPIC": True}

    # Sources are located in the same place as this recipe, copy them to the recipe
    exports_sources = "CMakeLists.txt", "src/*"

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def layout(self):
        cmake_layout(self)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def package_info(self):
        self.cpp_info.libs = ["hello"]

3.2 conan create

$ conan create . demo/testing
...
hello/0.1: Hello World Release!
  hello/0.1: _M_X64 defined
  ...

验证是否成功构建对应的conan软件包:

$ conan search
Existing package recipes:

hello/0.1@demo/testing

$ conan search hello/0.1@demo/testing
Existing packages for recipe hello/0.1@demo/testing:

Package_ID: 3fb49604f9c2f729b85ba3115852006824e72cab
    [options]
        shared: False
    [settings]
        arch: x86_64
        build_type: Release
        ...

conan create 命令接收与 conan install 相同的命令行参数,因此可以向其传递相同的设置和选项。如果我们执行以下命令行,就会为这些配置创建新的软件包二进制文件:

$ conan create . demo/testing -s build_type=Debug
...
hello/0.1: Hello World Debug!

$ conan create . demo/testing -o hello:shared=True
...
hello/0.1: Hello World Release!

这些新的软件包二进制文件也将存储在conan缓存中,随时可供这台计算机中的任何项目使用,我们可以通过以下方式查看它们:

$ conan search hello/0.1@demo/testing
Existing packages for recipe hello/0.1@demo/testing:

    Package_ID: 127af201a4cdf8111e2e08540525c245c9b3b99e
        [options]
            shared: True
        [settings]
            arch: x86_64
            build_type: Release
            ...
    Package_ID: 3fb49604f9c2f729b85ba3115852006824e72cab
        [options]
            shared: False
        [settings]
            arch: x86_64
            build_type: Release
            ...

    Package_ID: d057732059ea44a47760900cb5e4855d2bea8714
        [options]
            shared: False
        [settings]
            arch: x86_64
            build_type: Debug
            ...

4 软件包开发流程

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值