本文是基于对conan官方文档Workflows的翻译而来, 更详细的信息可以去查阅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
This section summarizes some possible layouts and workflows when using Conan together with other tools as an end-user for installing and consuming existing packages. To create your own packages, please refer to Creating Packages.
本节总结了作为最终用户使用 Conan
和其他工具安装和使用现有软件包时的一些可能布局和工作流程。要创建自己的软件包,请参阅 “Creating Packages”。
Whether you are working on a single configuration or a multi configuration project, in both cases, the recommended approach is to have a conanfile (either .py or .txt) at the root of your project.
无论是单配置项目还是多配置项目,推荐的做法都是在项目根目录下建立一个 conanfile(.py 或 .txt)
。
1 Single configuration
When working with a single configuration, your conanfile will be quite simple as shown in the examples and tutorials we have used so far in this user guide. For example, in Getting started, we showed how you can run the conan install … command inside the build folder resulting in the conaninfo.txt and conanbuildinfo.cmake files being generated there too. Note that the build folder is temporary, so you should exclude it from version control to exclude these temporary files.
在使用单一配置时,您的 conanfile 将非常简单,正如本用户指南中的示例和教程所示。例如,在 "Getting started"中,我们展示了如何在构建文件夹中运行 conan install …命令,从而在该文件夹中生成 conaninfo.txt 和 conanbuildinfo.cmake 文件。请注意,联编文件夹是临时文件,因此应将其从版本控制中排除,以排除这些临时文件。
Out-of-source builds are also supported. Let’s look at a simple example:
还支持源外构建。让我们来看一个简单的例子:
$ git clone https://github.com/conan-io/examples.git
$ cd libraries/poco
$ conan install ./md5 --install-folder=md5_build
This will result in the following layout:
这将产生以下布局:
md5_build
conaninfo.txt
conanbuildinfo.txt
conanbuildinfo.cmake
md5
CMakeLists.txt # If using cmake, but can be Makefile, sln...
README.md
conanfile.txt
md5.cpp
Now you are ready to build:
现在您已准备好构建了:
$ cd md5_build
$ cmake ../md5 -G "Visual Studio 15 Win64" # or other generator
$ cmake --build . --config Release
$ ./bin/md5
> c3fcd3d76192e4007dfb496cca67e13b
We have created a separate build configuration of the project without affecting the original source directory in any way. The benefit is that we can freely experiment with the configuration: We can clear the build folder and build another. For example, changing the build type to Debug:
我们为项目创建了一个独立的构建配置,但丝毫不影响原始源代码目录。这样做的好处是,我们可以自由地对配置进行实验: 我们可以清除构建文件夹,然后构建另一个文件夹。例如,将构建类型更改为调试:
$ rm -rf *
$ conan install ../md5 -s build_type=Debug
$ cmake ../md5 -G "Visual Studio 15 Win64"
$ cmake --build . --config Debug
$ ./bin/md5
> c3fcd3d76192e4007dfb496cca67e13b
2 Multi configuration
You can also manage different configurations, whether in-source or out of source, and switch between them without having to re-issue the conan install
command (Note however, that even if you did have to run conan install
again, since subsequent runs use the same parameters, they would be very fast since packages would already have been installed in the local cache rather than in the project)
您还可以管理不同的配置(无论是源内配置还是源外配置),并在它们之间切换,而无需重新发布 conan install
命令(请注意,即使您必须再次运行 conan install
,由于后续运行使用相同的参数,它们也会非常快,因为软件包已经安装在本地缓存中,而不是项目中。
$ git clone git@github.com:conan-io/examples
$ cd libraries/poco
$ conan install md5 -s build_type=Debug -if md5_build_debug
$ conan install md5 -s build_type=Release -if md5_build_release
$ cd md5_build_debug && cmake ../md5 -G "Visual Studio 15 Win64" && cd ../..
$ cd md5_build_release && cmake ../md5 -G "Visual Studio 15 Win64" && cd ../..
Note
You can either use the --install-folder
or -if
flags to specify where to generate the output files, or manually create the output directory and navigate to it before executing the conan install
command.
您可以使用 --install-folder
或 -if
标志指定生成输出文件的位置,或者在执行 conan install
命令前手动创建输出目录并导航到该目录。
So the layout will be:
因此,布局将会是:
md5_build_debug
conaninfo.txt
conanbuildinfo.txt
conanbuildinfo.cmake
CMakeCache.txt # and other cmake files
md5_build_release
conaninfo.txt
conanbuildinfo.txt
conanbuildinfo.cmake
CMakeCache.txt # and other cmake files
example-poco-timer
CMakeLists.txt # If using cmake, but can be Makefile, sln...
README.md
conanfile.txt
md5.cpp
Now you can switch between your build configurations in exactly the same way you do for CMake or other build systems, by moving to the folder in which the build configuration is located, because the Conan configuration files for that build configuration will also be there.
现在,您可以用与 CMake 或其他构建系统完全相同的方式在构建配置之间切换,只需移动到构建配置所在的文件夹即可,因为该构建配置的 Conan 配置文件也在该文件夹中。
$ cd md5_build_debug && cmake --build . --config Debug && cd ../..
$ cd md5_build_release && cmake --build . --config Release && cd ../..
Note that the CMake include() of your project must be prefixed with the current cmake binary directory, otherwise it will not find the necessary file:
请注意,您项目的 CMake include() 必须以当前 cmake 二进制目录为前缀,否则它将找不到所需的文件:
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
See also
There are two generators, cmake_multi
and visual_studio_multi
that could help to avoid the context switch and using Debug and Release configurations simultaneously. Read more about them in cmake_multi and visual_studio_multi
cmake_multi
和 visual_studio_multi
这两个生成器可以帮助避免上下文切换,并同时使用Debug和Release配置。在 cmake_multi 和 visual_studio_multi 中了解更多。