使用conan包 - 安装依赖项

2023-11-28 03:01
文章标签 安装 使用 依赖 conan

本文主要是介绍使用conan包 - 安装依赖项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用conan包 - 安装依赖项

  • 主目录 conan Using packages
  • 1 Requires
  • 2 Optional user/channel
  • 3 Overriding requirements
  • 4 Generators
  • 5 Options

本文是基于对conan官方文档Installing dependencies的翻译而来, 更详细的信息可以去查阅conan官方文档。

This section shows how to setup your project and manage dependencies (i.e., install existing packages) with Conan.
本节将介绍如何使用 Conan 设置项目和管理依赖关系(即安装现有软件包)。

主目录 conan Using packages

  • Installing dependencies
    • Requires
    • Generators
    • Options
  • Using profiles
  • Workflows
    • Single configuration
    • Multi configuration
  • Debugging packages

In Getting started we used the conan install command to download the Poco library and build an example.
在 "Getting started "部分,我们使用 conan install 命令下载了 Poco 库并构建了一个示例。

If you inspect the conanbuildinfo.cmake file that was created when running conan install, you can see there that there are many CMake variables declared. For example CONAN_INCLUDE_DIRS_ZLIB, that defines the include path to the zlib headers, and CONAN_INCLUDE_DIRS that defines include paths for all dependencies headers.
如果检查运行 conan install 时创建的 conanbuildinfo.cmake 文件,可以看到其中声明了许多 CMake 变量。例如,CONAN_INCLUDE_DIRS_ZLIB 定义了 zlib 头文件的包含路径,而 CONAN_INCLUDE_DIRS 则定义了所有依赖头文件的包含路径。
在这里插入图片描述
If you check the full path that each of these variables defines, you will see that it points to a folder under your folder. Together, these folders are the local cache. This is where package recipes and binary packages are stored and cached, so they don’t have to be retrieved again. You can inspect the local cache with conan search, and remove packages from it with conan remove command.
如果查看这些变量定义的完整路径,你会发现它们都指向 <userhome> 文件夹下的一个文件夹。这些文件夹就是本地缓存。这就是软件包recipes和二进制软件包的存储和缓存位置,因此无需再次检索。您可以使用 conan search 查看本地缓存,也可以使用 conan remove 命令删除缓存中的软件包。

If you navigate to the folders referenced in conanbuildinfo.cmake you will find the headers and libraries for each package.
如果您浏览 conanbuildinfo.cmake 中引用的文件夹,就会发现每个软件包的头文件和库。

If you execute a conan install poco/1.9.4@ command in your shell, Conan will download the Poco package and its dependencies (openssl/1.0.2t and zlib/1.2.11) to your local cache and print information about the folder where they are installed. While you can install each of your dependencies individually like that, the recommended approach for handling dependencies is to use a conanfile.txt file. The structure of conanfile.txt is described below.
如果在 shell 中执行 conan install poco/1.9.4@ 命令,Conan 会将 Poco 软件包及其依赖包(openssl/1.0.2tzlib/1.2.11)下载到本地缓存,并打印安装文件夹的相关信息。您可以像这样单独安装每个依赖项,但处理依赖项的推荐方法是使用 conanfile.txt 文件。conanfile.txt 的结构如下描述所示。

1 Requires

The required dependencies should be specified in the [requires] section. Here is an example:
应在 [requires] 部分指定所需的依赖项。下面是一个例子:

[requires]
mypackage/1.0.0@company/stable

Where:

  • mypackage is the name of the package which is usually the same as the project/library.
  • mypackage 是软件包的名称,通常与项目/库的名称相同。
  • 1.0.0 is the version which usually matches that of the packaged project/library. This can be any string; it does not have to be a number, so, for example, it could indicate if this is a “develop” or “master” version. Packages can be overwritten, so it is also OK to have packages like “nightly” or “weekly”, that are regenerated periodically.
  • 1.0.0 是版本,通常与打包项目/库的版本一致。它可以是任何字符串,不一定是数字,例如,它可以表示这是 "develop "还是 "master "版本。软件包可以被覆盖,因此也可以使用 "nightly "或 "weekly "这样的软件包,它们会定期重新生成。
  • company is the owner of this package. It is basically a namespace that allows different users to have their own packages for the same library with the same name.
  • company是该软件包的所有者。它基本上是一个命名空间,允许不同用户为同一个库拥有自己的同名软件包。
  • stable is the channel. Channels provide another way to have different variants of packages for the same library and use them interchangeably. They usually denote the maturity of the package as an arbitrary string such as “stable” or “testing”, but they can be used for any purpose such as package revisions (e.g., the library version has not changed, but the package recipe has evolved).
  • stable就是channelchannel为同一库提供了另一种拥有不同变体软件包并可互换使用的方式。它们通常以任意字符串(如 "stable "或 “testing”)表示软件包的成熟度,但也可用于任何目的,如软件包的修订(例如,库的版本没有变化,但软件包的recipe发生了变化)。

2 Optional user/channel

If the package was created and uploaded without specifying the user and channel you can omit the user/channel when specifying a reference:
如果创建和上传软件包时没有指定userchannel,则可以在指定引用时省略user/channel

[requires]
packagename/1.2.0

3 Overriding requirements

You can specify multiple requirements and override transitive “require’s requirements”. In our example, Conan installed the Poco package and all its requirements transitively:
您可以指定多个需求,并覆盖传递性的 “require’s requirements”。在我们的示例中,conan安装了 Poco 软件包及其所有需求:

  • openssl/1.0.2t
  • zlib/1.2.11

Tip
This is a good example of overriding requirements given the importance of keeping the OpenSSL library updated.
鉴于保持更新 OpenSSL 库的重要性,这是一个overriding requirements的好例子。
Consider that a new release of the OpenSSL library has been released, and a new corresponding Conan package is available. In our example, we do not need to wait until pocoproject (the author) generates a new package of POCO that includes the new OpenSSL library.
考虑到 OpenSSL 库的新版本已经发布,相应的新 Conan 软件包也已可用。在我们的示例中,我们不需要等到 pocoproject(作者)生成包含新 OpenSSL 库的 POCO 新软件包。

We can simply enter the new version in the [requires] section:
我们只需在【requires】部分输入新版本即可:

[requires]
poco/1.9.4
openssl/1.0.2u

The second line will override the openssl/1.0.2t required by POCO with the currently non-existent openssl/1.0.2u.
第二行将用目前不存在的 openssl/1.0.2u 覆盖 POCO 所需的 openssl/1.0.2t。

Another example in which we may want to try some new zlib alpha features: we could replace the zlib requirement with one from another user or channel.
另一个例子是,我们可能想尝试一些新的 zlib alpha 功能:我们可以用另一个user 或 channel的 zlib 需求来替换。

[requires]
poco/1.9.4
openssl/1.0.2u
zlib/1.2.11@otheruser/alpha

Note
You can use environment variable CONAN_ERROR_ON_OVERRIDE to raise an error for every overridden requirement not marked explicitly with the override keyword.
你可以使用环境变量 CONAN_ERROR_ON_OVERRIDE,为每一个未明确标记覆盖关键字的覆盖需求引发错误。

4 Generators

Conan reads the [generators] section from conanfile.txt and creates files for each generator with all the information needed to link your program with the specified requirements. The generated files are usually temporary, created in build folders and not committed to version control, as they have paths to local folders that will not exist in another machine. Moreover, it is very important to highlight that generated files match the given configuration (Debug/Release, x86/x86_64, etc) specified when running conan install. If the configuration changes, the files will change accordingly.
Conan 会读取 conanfile.txt 中的 [generators] 部分,并为每个生成器(generator)创建文件,其中包含将程序与指定要求链接到您自己程序所需的所有信息。生成的文件通常是临时文件,创建在构建文件夹中,不会提交到版本控制中,因为它们的路径指向本地文件夹,而这些文件夹在另一台机器中并不存在。此外,必须确保生成的文件与运行 conan install 时指定的配置(Debug/Release、x86/x86_64 等)相匹配。如果配置更改,文件也会相应更改。

For a full list of generators, please refer to the complete generators reference.、
有关generators的完整列表,请参阅完整的generators参考资料。

5 Options

We have already seen that there are some settings that can be specified during installation. For example, conan install .. -s build_type=Debug. These settings are typically a project-wide configuration defined by the client machine, so they cannot have a default value in the recipe. For example, it doesn’t make sense for a package recipe to declare “Visual Studio” as a default compiler because that is something defined by the end consumer, and unlikely to make sense if they are working in Linux.
我们已经看到,在安装过程中可以指定一些设置。例如,conan install ... -s build_type=Debug。这些设置通常是由客户机器定义的项目范围内的配置,因此它们在recipe中不可能有默认值。例如,在软件包配方中将 "Visual Studio "声明为默认编译器是不合理的,因为这是由最终用户定义的,如果他们在 Linux 中工作,就不可能将其作为默认编译器。

On the other hand, options are intended for package specific configuration that can be set to a default value in the recipe. For example, one package can define that its default linkage is static, and this is the linkage that should be used if consumers don’t specify otherwise.
另一方面,options用于软件包的特定配置,可以在recipe中设置为默认值。例如,一个软件包可以定义其默认链接为静态链接,如果消费者没有另行指定,就应该使用这种链接。

Note
You can see the available options for a package by inspecting the recipe with conan get <reference> command:
使用 conan get <reference> 命令查看配方,可以看到软件包的可用选项:

$ conan get poco/1.9.4@

To see only specific fields of the recipe you can use the conan inspect command instead:
如果只想查看配方的特定字段,可以使用 conan inspect 命令:

$ conan inspect poco/1.9.4@ -a=options
$ conan inspect poco/1.9.4@ -a=default_options

For example, we can modify the previous example to use dynamic linkage instead of the default one, which was static, by editing the [options] section in conanfile.txt:
例如,我们可以通过编辑 conanfile.txt 中的 [options] 部分,修改前面的示例,使用动态链接,而不是默认的静态链接:

[requires]
poco/1.9.4[generators]
cmake[options]
poco:shared=True # PACKAGE:OPTION=VALUE
openssl:shared=True

Install the requirements and compile from the build folder (change the CMake generator if not in Windows):
安装requirements并从构建文件夹编译(如果不是在 Windows 系统中,请更改 CMake 生成器):

$ conan install ..
$ cmake .. -G "Visual Studio 14 Win64"
$ cmake --build . --config Release

As an alternative to defining options in the conanfile.txt file, you can specify them directly in the command line:
除了在 conanfile.txt 文件中定义选项外,还可以直接在命令行中指定选项:

$ conan install .. -o poco:shared=True -o openssl:shared=True
# or even with wildcards, to apply to many packages
$ conan install .. -o *:shared=True

Conan will install the binaries of the shared library packages, and the example will link with them. You can again inspect the different binaries installed. For example, conan search zlib/1.2.11@.
Conan 将安装共享库软件包的二进制文件,示例将与它们链接。你可以再次检查安装的不同二进制文件。例如,Conan 搜索 zlib/1.2.11@。

Finally, launch the executable:
最后,加载可执行文件:

$ ./bin/md5

What happened? It fails because it can’t find the shared libraries in the path. Remember that shared libraries are used at runtime, so the operating system, which is running the application, must be able to locate them.
发生了什么?失败的原因是在路径中找不到共享库。请记住,共享库是在运行时使用的,因此运行应用程序的操作系统必须能够找到它们。

We could inspect the generated executable, and see that it is using the shared libraries. For example, in Linux, we could use the objdump tool and see the Dynamic section:
我们可以检查生成的可执行文件,看看它是否使用了共享库。例如,在 Linux 中,我们可以使用 objdump 工具查看动态部分:

$ cd bin
$ objdump -p md5
...
Dynamic Section:NEEDED               libPocoUtil.so.31NEEDED               libPocoXML.so.31NEEDED               libPocoJSON.so.31NEEDED               libPocoMongoDB.so.31NEEDED               libPocoNet.so.31NEEDED               libPocoCrypto.so.31NEEDED               libPocoData.so.31NEEDED               libPocoDataSQLite.so.31NEEDED               libPocoZip.so.31NEEDED               libPocoFoundation.so.31NEEDED               libpthread.so.0NEEDED               libdl.so.2NEEDED               librt.so.1NEEDED               libssl.so.1.0.0NEEDED               libcrypto.so.1.0.0NEEDED               libstdc++.so.6NEEDED               libm.so.6NEEDED               libgcc_s.so.1NEEDED               libc.so.6

这篇关于使用conan包 - 安装依赖项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/428942

相关文章

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

如何在Mac上安装并配置JDK环境变量详细步骤

《如何在Mac上安装并配置JDK环境变量详细步骤》:本文主要介绍如何在Mac上安装并配置JDK环境变量详细步骤,包括下载JDK、安装JDK、配置环境变量、验证JDK配置以及可选地设置PowerSh... 目录步骤 1:下载JDK步骤 2:安装JDK步骤 3:配置环境变量1. 编辑~/.zshrc(对于zsh

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的