创建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

智能推荐

电阻电位器电子尺信号采集转换为标准Modbus TCP协议-程序员宅基地

文章浏览阅读140次。产品概述:WJ183产品是一种物联网和工业以太网采集模块,实现了传感器与网络之间形成透明的数据交互。可以将传感器的模拟量数据转发到网络。

props和state-程序员宅基地

文章浏览阅读40次。2019独角兽企业重金招聘Python工程师标准>>> ..._联动组件数据刷新 props 关联

RxPosed:Android逆向工程与模块化框架的革新-程序员宅基地

文章浏览阅读404次,点赞3次,收藏7次。RxPosed:Android逆向工程与模块化框架的革新项目地址:https://gitcode.com/Thehepta/rxposedRxPosed是一个基于XPosed框架的,用于Android应用逆向工程和动态 hook 的库。它利用了ReactiveX(一个用于处理异步数据流的库)的灵活性和强大的功能,为开发者提供了一种全新的方式来操纵应用程序的行为。该项目源自GitCode并开源,...

【2024】基于springboot的二手商品拍卖商城系统设计与实现-程序员宅基地

文章浏览阅读888次,点赞28次,收藏25次。基于SpringBoot的拍卖商城系统设计与实现的课题背景、目的、意义和研究方法如下:课题背景:随着互联网技术的快速发展和移动设备的广泛普及,电子商务已经成为现代社会重要的商业模式之一。在线拍卖作为一种新型的电子商务模式,它结合了传统的拍卖方式与互联网的技术优势,为用户提供了更加便捷、高效的拍卖体验。SpringBoot作为一个轻量级的Java开发框架,以其易用性、灵活性和高度自动化的特点,在开发企业级应用程序中得到了广泛的应用。因此,基于SpringBoot开发拍卖商城系统,不仅可以满足市场对于高效、

python 调用c++ 动态链接库_python中怎样调用c语言动态链接库-程序员宅基地

文章浏览阅读1.8k次。Python调用DLL例子示例一首先,在创建一个DLL工程(本人是在VS 2005中创建),头文件:[cpp] view plain copy //hello.h #ifdef EXPORT_HELLO_DLL #define HELLO_API __declspec(dllexport) #else _python中怎样调用c语言动态链接库

使用PyQt开发图形界面Python应用专栏目录_pythonqt图形界面开发-程序员宅基地

文章浏览阅读1w次,点赞20次,收藏74次。本专栏为收费专栏的文章目录,对应的免费专栏为《[PyQt入门知识目录](https://blog.csdn.net/laoyuanpython/category_9702362.html)》,两个专栏都为基于PyQt的Python图形界面开发基础教程,只是收费专栏中的内容介绍更深入、案例代码更全。Qt和PyQt介绍的很多部件属性及字典取值在Qt及PyQt官方文档以及网上都没有,老猿通过大量研究、测试将相关内容弄清楚,因此本专栏适合有一定Python基础的同仁零基础学习Python图形界面开发,也适合学习_pythonqt图形界面开发

随便推点

RSS技术值得关注的技术_技术rss-程序员宅基地

文章浏览阅读2.3k次。RSS主演互联网变局 盛大新浪只是配角王翌 盛大和新浪的喜筵已近尾声,媒体上铺天盖地猜测着互联网新寡头时代的下一出戏谁来抢?是否该网易或腾讯甚至百度出手了?还有谁没收购?还有谁没被收购? 在很多时候,资本确实有改变产业格局的能力和能量。但是,最富于变化的、发展最快的互联网产业在更多时候是财富的创造者而非资本的附庸!主导互联网发展的,是技术,而不是股价;推动互联网前进的,_技术rss

自动驾驶中的行为预测(BP)技术_autoware行为预测-程序员宅基地

文章浏览阅读672次,点赞2次,收藏2次。行为预测是指根据当前的环境信息和历史数据,预测其他交通参与者(如行人、车辆)未来的行为。其主要目标是提前识别潜在的危险情况并采取适当的措施,以确保自动驾驶车辆的安全性和可靠性。此外,行为预测还能够为自动驾驶系统提供更高效的路径规划和决策依据。行为预测研究中常用的数据集包括Nuscenes、KITTI、ApolloScape等。这些数据集包含了丰富的场景信息和真实的交通数据,可以用于模型的训练和评估。常用的行为预测评估指标包括准确率、召回率、F1值等。_autoware行为预测

Qt6 for Android 环境搭建_qt6的andiord-程序员宅基地

文章浏览阅读1.6k次,点赞3次,收藏5次。JDK、Android SDK、NDK 安装下载,AVD 的创建,以及环境测试等_qt6的andiord

云课堂缺勤补签软件_在线课堂回顾 | 一小时玩转高通量虚拟筛选-程序员宅基地

文章浏览阅读1.6k次。云端E课堂简介云端E课堂是云端软件推出的针对云E算力平台使用的免费在线课堂,覆盖量化计算、生物科技、人工智能等领域的实操演示,云端E课堂将邀请诸多领域的大咖为学员提供最贴近实操的内容。课程概况基于结构的虚拟筛选是发现苗头化学物的一个重要计算方法。但海量化合物库使得传统的虚拟筛选方法显得效率不足。虽然哈佛大学医学院的研究人员开发的开源药物发现平台Virtualflow就是针对这个问题的一种..._在线虚拟筛选

2021-02-22_unssigned class[ff00] : device-程序员宅基地

文章浏览阅读1.8k次,点赞7次,收藏12次。jacinto 内核驱动 – 43.2.2.10. PCIe端点介绍集成在Jacinto 7中的PCIe控制器IPs能够在根复杂模式(主机)或端点模式(设备)下运行。当在End Point (EP)模式下操作时,控制器可以配置为根据用例使用的任何功能(’ Test endpoint ‘和’ NTB '是目前Linux内核中唯一支持的PCIe EP功能)。框图以下是端点模式框架框图:Features of J7ESJ7ES的特点PCIe子系统有四个实例。以下是一些主要功能:•每个实例都可以配_unssigned class[ff00] : device

ksz8863调试总线,-程序员宅基地

文章浏览阅读6.7k次,点赞3次,收藏21次。ksz8863是一款交换芯片,结构如下:有2个完整的MAC + PHY,还有第三路网口,只有一个MAC3,右边是switch逻辑,下边是控制接口,支持i2c/spi/smi1.片子的rmii/mii接口支持2种模式, MAC模式 加 PHY模式, 注意,工作模式是相对于它自身所起的功能所说的,如果它是通过rmii接stm32f437的MAC,它起到一个外置PHY的作用,那它...

推荐文章

热门文章

相关标签