创建conan包-入门指南_conan2.0 创建-程序员宅基地

技术标签: 创建conan包-入门指南  创建conan包  conan  

本文是基于对conan官方文档Creating packages - Getting started翻译而来, 更详细的信息可以去查阅conan官方文档。

1 Getting started

This section introduces how to create your own Conan packages, explain conanfile.py recipes and the commands to build packages from sources in your computer.
本节将介绍如何创建自己的conan软件包,解释 conanfile.py recipes和从计算机源代码构建软件包的命令。

1.1 Important

This is a tutorial section. You are encouraged to execute these commands. For this concrete example, you will need CMake installed in your path. It is not strictly required by Conan to create packages, you can use other build systems (as VS, Meson, Autotools and even your own) to do that, without any dependency to CMake.
这是tutorial部分。我们鼓励你执行这些命令。在此具体示例中,您需要在路径中安装 CMake。conan并不严格要求使用 CMake 创建软件包,您可以使用其他构建系统(如 VS、Meson、Autotools 甚至您自己的系统)来创建软件包,而无需依赖 CMake。

Some of the features used in this section are still under development, like CMakeToolchain or cmake_layout(), while they are recommended and usable and we will try not to break them in future releases, some breaking changes might still happen if necessary to prepare for the Conan 2.0 release.
本节中使用的一些功能仍在开发中,如 CMakeToolchaincmake_layout(),虽然它们是推荐和可用的,而且我们会尽量避免在未来的版本中破坏它们,但如果有必要,为了准备柯南 2.0 的发布,一些破坏性的更改仍可能发生。

1.2 conan new

Using the conan new command will create a “Hello World” C++ library example project for us:
使用 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

The generated files are:

  • conanfile.py: On the root folder, there is a conanfile.py which is the main recipe file, responsible for defining how the package is built and consumed.
  • conanfile.py: 在根文件夹中有一个 conanfile.py,它是主要的recipe文件,负责定义软件包的构建和使用方式。
  • CMakeLists.txt: A simple generic CMakeLists.txt, with nothing specific about Conan in it.
  • CMakeLists.txt: 一个简单的通用 CMakeLists.txt,其中没有任何关于柯南的特定内容。
  • src folder: the src folder that contains the simple C++ “hello” library.
  • src 文件夹:包含简单 C++"hello "库的 src 文件夹。
  • (optional) test_package folder: contains an example application that will require and link with the created package. It is not mandatory, but it is useful to check that our package is correctly created.
  • (可选)test_package 文件夹:包含一个示例应用程序,该应用程序将需要并链接已创建的软件包。这不是必须的,但对于检查我们是否正确创建了软件包很有用。

1.3 分析conanfile.py

Let’s have a look at the package recipe conanfile.py:
让我们来看看软件包recipe conanfile.py:

from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout


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

    # Optional metadata
    license = "<Put the package license here>"
    author = "<Put your name here> <And your email here>"
    url = "<Package recipe repository url here, for issues about the package>"
    description = "<Description of Hello here>"
    topics = ("<Put some tag here>", "<here>", "<and here>")

    # 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/*", "include/*"

    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"]

Let’s explain a little bit about this recipe:
让我们来解释一下这个recipe:

  • The binary configuration is composed by settings and options. See more in this section. When something changes in the configuration, the resulting binary built and packaged will be different:
  • 二进制配置由设置和选项组成。请参阅本节的更多内容。当配置发生变化时,生成和打包的二进制文件也会不同:
    • settings are project wide configuration, that cannot be defaulted in recipes, like the OS or the architecture.
    • 设置是项目范围内的配置,不能在recipes中默认,如操作系统或架构。
    • options are package specific configuration and can be defaulted in recipes, in this case we have the option of creating the package as a shared or static library, being static the default.
    • 在本例中,我们可以选择将软件包创建为共享库或静态库,默认为静态库。
  • The exports_sources attribute defines which sources are exported together with the recipe, these sources become part of the package recipe (there are other mechanisms that don’t do this, which will be explained later).
  • exports_sources 属性定义了哪些源会与配方一起导出,这些源会成为软件包配方的一部分(还有其他机制不这样做,稍后解释)。
  • The config_options() method (together with configure()) allows to fine tune the binary configuration model. For example, in Windows there is no fPIC option, so it can be removed.
  • config_options()方法(与configure()一起)可以对二进制配置模型进行微调。例如,在 Windows 中没有 fPIC 选项,因此可以将其删除。
  • The generate() method prepares the build of the package from source. In this case, it could be simplified to an attribute generators = “CMakeToolchain”, but it is left to show this important method. In this case, the execution of CMakeToolchain generate() method will create a conan_toolchain.cmake file that maps the Conan settings and options to CMake syntax.
  • generate() 方法准备从源代码构建软件包。在本例中,它可以简化为属性 generators = "CMakeToolchain",但为了显示这个重要的方法,我们还是把它留了下来。在这种情况下,执行 CMakeToolchain generate() 方法将创建一个 conan_toolchain.cmake 文件,将 Conan 设置和选项映射为 CMake 语法。
  • The build() method uses the CMake wrapper to call CMake commands. It is a thin layer that will manage to pass in this case the -DCMAKE_TOOLCHAIN_FILE= /conan_toolchain.cmake argument, plus other possible arguments, like -DCMAKE_BUILD_TYPE= if necessary. It will configure the project and build it from source. The actual arguments that will be used are obtained from a generated CMakePresets.json file.
  • build() 方法使用 CMake 封装器调用 CMake 命令。它是一个薄层,在本例中将传递 -DCMAKE_TOOLCHAIN_FILE=<path>/conan_toolchain.cmake 参数,以及其他可能的参数,如 -DCMAKE_BUILD_TYPE=<config> (如有必要)。它将配置项目并从源代码开始构建。实际使用的参数将从生成的 CMakePresets.json 文件中获取。
  • The package() method copies artifacts (headers, libs) from the build folder to the final package folder. It can be done with bare “copy” commands, but in this case it is leveraging the already existing CMake install functionality (if the CMakeLists.txt didn’t implement it, it is easy to write self.copy() commands in this package() method.
  • package() 方法会将构建文件夹中的工件(头文件、库文件)复制到最终的 package 文件夹中。这可以通过简单的 "复制 "命令来完成,但在本例中,它是在利用 CMake 已经存在的安装功能(如果 CMakeLists.txt 没有实现该功能,在此 package() 方法中编写 self.copy() 命令也很容易)。
  • Finally, the package_info() method defines that consumers must link with a “hello” library when using this package. Other information as include or lib paths can be defined as well. This information is used for files created by generators (as CMakeDeps) to be used by consumers. Although this method implies some potential duplication with the build system output (CMake could generate xxx-config.cmake files), it is important to define this, as Conan packages can be consumed by any other build system, not only CMake.
  • 最后,package_info() 方法定义了用户在使用该软件包时必须与 "hello "库链接。还可以定义其他信息,如 include 或 lib 路径。这些信息用于生成器(如 CMakeDeps)创建的文件,以便用户使用。虽然这种方法可能会与联编系统的输出重复(CMake 可能会生成 xxx-config.cmake 文件),但定义这一点非常重要,因为conan软件包可以被任何其他联编系统使用,而不仅仅是 CMake

1.4 test_package

The contents of the test_package folder is not critical now for understanding how packages are created, the important bits are:
现在,test_package 文件夹的内容对于了解软件包的创建过程并不重要,重要的是这些内容:

  • test_package folder is different from unit or integration tests. These tests are “package” tests, and validate that the package is properly created, and that the package consumers will be able to link against it and reuse it.
  • test_package 文件夹与单元测试或集成测试不同。这些测试是 "package"测试,用于验证软件包是否已正确创建,以及软件包用户是否能链接并重用软件包。
  • It is a small Conan project itself, it contains its own conanfile.py, and its source code including build scripts, that depends on the package being created, and builds and execute a small application that requires the library in the package.
  • 它本身是一个小型 Conan 项目,包含自己的 conanfile.py、源代码(包括构建脚本),依赖于正在创建的软件包,并构建和执行一个需要软件包中库的小型应用程序。
  • It doesn’t belong to the package. It only exist in the source repository, not in the package.
  • 它不属于软件包。它只存在于源代码库中,而不在软件包中。

Let’s build the package from sources with the current default configuration (default profile), and then let the test_package folder test the package:
让我们使用当前的默认配置(默认配置文件)从源代码构建软件包,然后让 test_package 文件夹测试软件包:

1.5 conan create

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

If “Hello world Release!” is displayed, it worked. This is what has happened:
如果显示 “Hello world Release!”,就说明成功了。情况就是这样:

  • The conanfile.py together with the contents of the src folder have been copied (exported in Conan terms) to the local Conan cache.
  • conanfile.pysrc 文件夹中的内容已被复制(用 Conan 术语来说就是导出)到本地 Conan 缓存中。
  • A new build from source for the hello/0.1@demo/testing package starts, calling the generate(), build() and package() methods. This creates the binary package in the Conan cache.
  • 开始从源代码构建 hello/0.1@demo/testing 软件包,调用 generate()build()package() 方法。这将在 Conan 缓存中创建二进制包。
  • Moves to the test_package folder and executes a conan install + conan build + test() method, to check if the package was correctly created. This happens automatically whenever a test_package folder is supplied next to the conanfile.py being processed.
  • 移动到 test_package 文件夹,执行 conan install + conan build + test() 方法,检查是否正确创建了软件包。只要在正在处理的 conanfile.py 旁边提供 test_package 文件夹,就会自动执行此操作。

1.6 验证conan包

We can now validate that the recipe and the package binary are in the cache:
现在我们可以验证缓存中是否有recipe和软件包二进制文件:

$ 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
        ...

The conan create command receives the same command line parameters as conan install so you can pass to it the same settings and options. If we execute the following lines, we will create new package binaries for those configurations:
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!

These new package binaries will be also stored in the Conan cache, ready to be used by any project in this computer, we can see them with:
这些新的软件包二进制文件也将存储在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
            ...
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014100559/article/details/134701427

智能推荐

流程控制总结_java流程控制实验总结-程序员宅基地

文章浏览阅读1.7k次,点赞2次,收藏4次。//第三课,流程控制 ctrl + shift +o 可以导入从键盘获取输入的资源 //单分支选择结构 if(条件表达式){ 代码块 } //双分支选择结构 if(条件表达式){ 代码块1 } else{ 代码块2 } //多分支选择结构 if(条件表达式1){ 代码块1 } else if(条件表达式2){ 代码块_java流程控制实验总结

使用Python为时间序列预测创建ARIMA模型_model_fit = model.fit(disp=0)-程序员宅基地

文章浏览阅读6.9k次,点赞6次,收藏50次。 如何在Python中为时间序列预测创建ARIMA模型 ARIMA模型是一种流行且广泛使用的时间序列预测统计方法。ARIMA是AutoRegressive Integrated Moving Average的缩写。它是一类模型,它捕获时间序列数据中的一套不同的标准时间结构。 ARIMA模型的信息还可以参考这里 ..._model_fit = model.fit(disp=0)

靶机渗透测试实战(七)——FristiLeaks渗透实战-程序员宅基地

文章浏览阅读3.3k次,点赞6次,收藏19次。目录一. 实验环境二. 实验流程三. 实验步骤(一)信息收集——主机发现1. 查看Kali的IP信息;(IP:192.168.37.131)2. 查看靶机页面,IP地址为:192.168.37.146;3. 扫描主机(netdiscover)(二)信息收集——端口扫描1. 扫描端口(masscan)2. 详细扫描端口信息(nmap)(三)渗透测试80..._fristileaks

ROS学习(1)简介和文件系统-程序员宅基地

文章浏览阅读151次。目录1.ROS简介1.1机器人时代的到来中国大学MOOC—《机器人操作系统入门》配套讲义1.ROS简介机器人操作系统(Robot Operating System, ROS)是一个应用于机器人上的操作系统,它操作方便、功能强大,特别适用于机器人这种多节点多任务的复杂场景。 因此自ROS诞生以来,受到了学术界和工业界的欢迎,如今已经广泛应用于机械臂、移动底盘、无人机、无人车等许多种类的机器人上。1.1机器人时代的到来...

【SVM时序预测】基于支持向量机的时间序列预测(libsvm)附matlab代码_支持向量机 时间序列-程序员宅基地

文章浏览阅读452次。在现代数据科学和机器学习领域中,时间序列预测是一个非常重要的问题。时间序列预测可以用于许多应用,例如股票价格预测、天气预测、交通流量预测等。支持向量机(SVM)是一种广泛使用的机器学习算法,可以用于时间序列预测。本文将介绍基于支持向量机的时间序列预测算法步骤。数据预处理在进行时间序列预测之前,需要对数据进行预处理。这包括数据清洗、数据平滑、数据归一化等。数据清洗可以去除异常值、缺失值等。数据平滑可以减小数据的噪声,使数据更加平滑。数据归一化可以将数据缩放到相同的范围内,以便更好地进行比较和分析。_支持向量机 时间序列

ogg oracle 测试kafka_基于OGG 实现Oracle到Kafka增量数据实时同步-程序员宅基地

文章浏览阅读128次。背景在大数据时代,存在大量基于数据的业务。数据需要在不同的系统之间流动、整合。通常,核心业务系统的数据存在OLTP数据库系统中,其它业务系统需要获取OLTP系统中的数据。传统的数仓通过批量数据同步的方式,定期从OLTP系统中抽取数据。但是随着业务需求的升级,批量同步无论从实时性,还是对在线OLTP系统的抽取压力,都无法满足要求。需要实时从OLTP系统中获取数据变更,实时同步到下游业务系统。本文基于..._kafka ogg oracle客户端

随便推点

python3 爬虫 零基础快速上手(爬虫示例)-程序员宅基地

文章浏览阅读3.9w次,点赞32次,收藏220次。(补1:由于csdn页面重构了,看到有很多人浏览,特地更新一下获取内容补充的字都为绿字)Python是一种面向对象的解释型计算机程序设计语言,类似c,java ,php,JavaScript一种语言,比如,完成同一个任务,C语言要写1000行代码,Java只需要写100行,而Python可能只要20行。Python是一种高级的语言。一:python 安装:首先,根据你的Windows版本(..._python3 爬虫

用 Python解析HTML页面_python解析html用哪个模块-程序员宅基地

文章浏览阅读1.4k次。本文介绍了 Python 中常用的三种 HTML 解析方式:XPath 解析、CSS 选择器解析和正则表达式解析。在具体的应用过程中,我们可以根据需要选择不同的解析方式。XPath 解析适用于对 HTML 页面的层次结构进行解析,能够比较方便地定位页面元素;CSS 选择器解析适用于对 HTML 页面的类名、id 等属性进行解析,可以快速定位元素;正则表达式解析适用于对 HTML 页面的标签和文本进行解析,可以灵活地处理页面元素。希望本文能够对大家进行 HTML 解析方面的学习和实践有所帮助。_python解析html用哪个模块

使用CDN的方式使用Vue 和 iview构建vue项目_cdn方式 vue怎么封装组件-程序员宅基地

文章浏览阅读1w次,点赞7次,收藏19次。使用CDN的方式使用Vue 和 iview构建vue项目一、CDN的方式是什么二、为什么采用cdn的方式三、过程1 引入vue.js和iview。2 基本功能3 组件功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaT......_cdn方式 vue怎么封装组件

程序员写春联,秒杀全场!网友:不愧是聪明绝顶的“程序猿”!_years months weeks day day no bug java python c++ -程序员宅基地

文章浏览阅读4.2k次,点赞3次,收藏4次。转眼间,春节就要过完了,回想起春节即将来临之前,每家每户都贴起了对联,很多机构、公司、学校也贴出了具有代表性的春联。有些春联让人看了,可谓是惊叹连连、啧啧称奇。那当然身为程序员的一家,也不能输了气势,谁说程序员只会敲代码,程序员敲起代码来也是秒杀全场,网友连连夸赞:不愧是聪明绝顶的”程序猿“!很明显,这是一副程序员写的春联。上联:Python Java C++ Line Line So..._years months weeks day day no bug java python c++ line line so easy

谷歌chrome运行activeX控件_ie_tab_multi_extension-程序员宅基地

文章浏览阅读1.5w次,点赞3次,收藏9次。在谷歌chrome浏览器下,安装IE_Tab_Multi_extension_1_0_0_1控件即可具体操作:将IE_Tab_Multi_extension_1_0_0_1 拖入谷歌浏览器然后点击:添加即可谷歌浏览器不能直接用activeX原因:因为Activex是由微软开发,因而目前只支持原生态支持的IE,最新版Edge已经不再支持了。其他浏览器想要支持activex, 需要额外做一些设置或安装补丁包,其中谷歌浏览器的话,需要安装 IE-Tab-Multi控件IE_Tab_Multi_exte_ie_tab_multi_extension

07-06-Exchange Server 2019-配置-新建数据库-DAG-程序员宅基地

文章浏览阅读778次。[在此处输入文章标题] 《系统工程师实战培训》 -07-部署邮件系统 -06-Exchange Server 2019- 配置-新建数据库-DAG 作者:学 无 止 境 QQ交流群:454544014 新建数据库-db_vip_01, 新建数据库-db_vip_02 在三台邮件服务器上新建数据库目录: db_Mailbox PowerShell m..._exchange 2019 dag 部署

推荐文章

热门文章

相关标签