使用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 则定义了所有依赖头文件的包含路径。
使用conan包 - 安装依赖项_第1张图片
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.
如果查看这些变量定义的完整路径,你会发现它们都指向 文件夹下的一个文件夹。这些文件夹就是本地缓存。这就是软件包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 command:
使用 conan get 命令查看配方,可以看到软件包的可用选项:

$ 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.31
 NEEDED               libPocoXML.so.31
 NEEDED               libPocoJSON.so.31
 NEEDED               libPocoMongoDB.so.31
 NEEDED               libPocoNet.so.31
 NEEDED               libPocoCrypto.so.31
 NEEDED               libPocoData.so.31
 NEEDED               libPocoDataSQLite.so.31
 NEEDED               libPocoZip.so.31
 NEEDED               libPocoFoundation.so.31
 NEEDED               libpthread.so.0
 NEEDED               libdl.so.2
 NEEDED               librt.so.1
 NEEDED               libssl.so.1.0.0
 NEEDED               libcrypto.so.1.0.0
 NEEDED               libstdc++.so.6
 NEEDED               libm.so.6
 NEEDED               libgcc_s.so.1
 NEEDED               libc.so.6

你可能感兴趣的:(conan,使用conan包,安装依赖项,使用conan包-安装依赖项)