构建用于多种配置:Release、Debug、Static 和 Shared

请首先克隆源代码以重新创建此项目。你可以在 GitHub 的 examples2 仓库中找到它们

$ git clone https://github.com/conan-io/examples2.git
$ cd examples2/tutorial/consuming_packages/different_configurations

到目前为止,我们构建了一个简单的 CMake 项目,该项目依赖于 zlib 库,并了解了 tool_requires,这是一种特殊的 requirements,用于构建工具,如 CMake。在这两种情况下,我们都没有在任何地方指定我们想要在 ReleaseDebug 模式下构建应用程序,或者我们是否想要链接到 静态共享 库。那是因为 Conan,如果没有另行指示,将使用在“默认 profile”中声明的默认配置。这个默认 profile 是在第一个示例中当我们运行 conan profile detect 命令时创建的。Conan 将此文件存储在 /profiles 文件夹中,该文件夹位于 Conan 用户主目录中。你可以通过运行 conan config home 命令来获取 Conan 用户主目录的位置,然后显示 /profiles 文件夹中默认 profile 的内容来检查你的默认 profile 的内容

$ conan config home
Current Conan home: /Users/tutorial_user/.conan2

# output the file contents
$ cat /Users/tutorial_user/.conan2/profiles/default
[settings]
os=Macos
arch=x86_64
compiler=apple-clang
compiler.version=14.0
compiler.libcxx=libc++
compiler.cppstd=gnu11
build_type=Release
[options]
[tool_requires]
[env]

# The default profile can also be checked with the command "conan profile show"

正如你所看到的,profile 有不同的部分。[settings] 部分是包含有关操作系统、架构、编译器和构建配置等信息的部分。

当你调用 Conan 命令并设置 --profile 参数时,Conan 将从 profile 中获取所有信息并将其应用于你要构建或安装的包。如果你不指定该参数,则等同于使用 --profile=default 调用它。这两个命令的行为将相同

$ conan install . --build=missing
$ conan install . --build=missing --profile=default

你可以存储不同的 profile,并使用它们来构建不同的设置。例如,使用 build_type=Debug,或将 tool_requires 添加到你使用该 profile 构建的所有包中。我们将创建一个 debug profile 来尝试使用不同的配置进行构建

<conan home>/profiles/debug
[settings]
os=Macos
arch=x86_64
compiler=apple-clang
compiler.version=14.0
compiler.libcxx=libc++
compiler.cppstd=gnu11
build_type=Debug

修改设置:为应用程序及其依赖项使用 Debug 配置

使用 profile 不是设置你要使用的配置的唯一方法。你还可以在 Conan 命令中使用 --settings 参数覆盖 profile 设置。例如,你可以使用 Debug 配置而不是 Release 配置来构建先前示例中的项目。

在构建之前,请检查我们是否修改了先前示例中的源代码,以显示源代码构建时使用的构建配置

#include <stdlib.h>
...

int main(void) {
    ...
    #ifdef NDEBUG
    printf("Release configuration!\n");
    #else
    printf("Debug configuration!\n");
    #endif

    return EXIT_SUCCESS;
}

现在让我们为 Debug 配置构建我们的项目

$ conan install . --output-folder=build --build=missing --settings=build_type=Debug

正如我们上面解释的那样,这等同于拥有 debug profile 并使用 --profile=debug 参数而不是 --settings=build_type=Debug 参数运行这些命令。

这个 conan install 命令将检查我们在本地缓存(Zlib)中是否已经有 Debug 配置所需的库,如果没有,则获取它们。它还将更新 conan_toolchain.cmakeCMakePresets.json 文件中的构建配置,CMakeToolchain 生成器会创建这些文件,以便当我们构建应用程序时,它以 Debug 配置构建。现在像你在之前的示例中那样构建你的项目,并在输出中检查它是如何在 Debug 配置中构建的

Windows
# assuming Visual Studio 15 2017 is your VS version and that it matches your default profile
$ cd build
$ cmake .. -G "Visual Studio 15 2017" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
$ cmake --build . --config Debug
$ Debug\compressor.exe
Uncompressed size is: 233
Compressed size is: 147
ZLIB VERSION: 1.2.11
Debug configuration!
Linux, macOS
$ cd build
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Debug
$ cmake --build .
$ ./compressor
Uncompressed size is: 233
Compressed size is: 147
ZLIB VERSION: 1.2.11
Debug configuration!

修改选项:将应用程序依赖项链接为共享库

到目前为止,我们一直在我们的应用程序中静态链接 Zlib。那是因为在 Zlib 的 Conan 包中,有一个属性设置为默认以该模式构建。我们可以通过使用 --options 参数将 shared 选项设置为 True,从而将链接从 static 更改为 shared。为此,请运行

$ conan install . --output-folder=build --build=missing --options=zlib/1.2.11:shared=True

这样做,Conan 将安装 Zlib 共享库,生成用于使用它们进行构建的文件,以及在运行应用程序时定位这些动态库所需的文件。

注意

选项是按包定义的。在本例中,我们定义了我们想要特定版本的 zlib/1.2.11 作为共享库。如果我们有其他依赖项,并且我们希望我们所有的依赖项(在可能的情况下)都作为共享库,我们将使用 -o *:shared=True,其中 * 模式匹配所有包引用。

在配置为将 Zlib 链接为共享库后,让我们再次构建应用程序

Windows
$ cd build
# assuming Visual Studio 15 2017 is your VS version and that it matches your default profile
$ cmake .. -G "Visual Studio 15 2017" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
$ cmake --build . --config Release
...
[100%] Built target compressor
Linux, Macos
$ cd build
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake --build .
...
[100%] Built target compressor

现在,如果你尝试运行编译后的可执行文件,你将看到一个错误,因为可执行文件找不到我们刚刚安装的 Zlib 共享库。

Windows
$ Release\compressor.exe
(on a pop-up window) The code execution cannot proceed because zlib1.dll was not found. Reinstalling the program may fix this problem.
# This error depends on the console being used and may not always pop up.
# It could run correctly if the console gets the zlib dll from a different path.
Linux
$ ./compressor
./compressor: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
Macos
$ ./compressor
./compressor: dyld[41259]: Library not loaded: @rpath/libz.1.dylib

这是因为共享库(Windows 中的 .dll,OSX 中的 .dylib 和 Linux 中的 .so)是在运行时加载的。这意味着应用程序可执行文件需要知道运行时所需的共享库在哪里。在 Windows 上,动态链接器将在同一目录,然后在 PATH 目录中搜索。在 OSX 上,它将在 DYLD_LIBRARY_PATH 中声明的目录中搜索,而在 Linux 上将使用 LD_LIBRARY_PATH

Conan 提供了一种机制来定义这些变量,并使可执行文件能够找到和加载这些共享库。这种机制是 VirtualRunEnv 生成器。如果你检查输出文件夹,你将看到 Conan 生成了一个名为 conanrun.sh/bat 的新文件。这是当我们激活 shared 选项执行 conan install 时自动调用 VirtualRunEnv 生成器的结果。这个生成的脚本将设置 PATHLD_LIBRARY_PATHDYLD_LIBRARY_PATHDYLD_FRAMEWORK_PATH 环境变量,以便可执行文件可以找到共享库。

激活虚拟环境,并再次运行可执行文件

Windows
$ conanrun.bat
$ Release\compressor.exe
Uncompressed size is: 233
Compressed size is: 147
...
Linux, macOS
$ source conanrun.sh
$ ./compressor
Uncompressed size is: 233
Compressed size is: 147
...

就像之前使用 VirtualBuildEnv 生成器的示例一样,当我们运行 conanrun.sh/bat 脚本时,会创建一个名为 deactivate_conanrun.sh/bat 的停用脚本,以恢复环境。source 或运行它来执行此操作

Windows
$ deactivate_conanrun.bat
Linux, macOS
$ source deactivate_conanrun.sh

设置和选项之间的区别

你可能已经注意到,为了在 DebugRelease 配置之间切换,我们使用了 Conan 设置,但是当我们为我们的可执行文件设置 shared 模式时,我们使用了 Conan 选项。请注意 设置选项 之间的区别

  • 设置 通常是由客户端机器定义的项目范围的配置。诸如操作系统、编译器或构建配置之类的事物对于多个 Conan 包来说是通用的,并且仅为其中一个包定义一个默认值是没有意义的。例如,Conan 包声明“Visual Studio”作为默认编译器是没有意义的,因为这是由最终消费者定义的,并且如果他们在 Linux 中工作,则不太可能具有意义。

  • 选项 旨在用于包特定的配置,可以在 recipe 中设置为默认值。例如,一个包可以定义其默认链接是静态的,这是如果消费者没有另行指定则应使用的链接。

引入包 ID 的概念

当使用具有不同 设置选项 的包(如 Zlib)时,你可能想知道 Conan 如何确定要从远程仓库检索哪个二进制文件。答案在于 package_id 的概念。

package_id 是 Conan 用于确定包的二进制兼容性的标识符。它基于多个因素计算,包括包的 设置选项 和依赖项。当你修改任何这些因素时,Conan 会计算一个新的 package_id 以引用相应的二进制文件。

以下是该过程的分解

  1. 确定设置和选项:Conan 首先检索用户的输入设置和选项。这些可以来自命令行或 profile,例如 –settings=build_type=Debug–profile=debug

  2. 计算包 ID:使用 设置选项 和依赖项的当前值,Conan 计算一个哈希值。此哈希值用作 package_id,表示二进制包的唯一身份。

  3. 获取二进制文件:然后,Conan 检查其缓存或指定的远程仓库中是否具有计算出的 package_id 的二进制包。如果找到匹配项,它将检索该二进制文件。如果找不到,Conan 可以从源代码构建包或指示二进制文件丢失。

在我们的教程上下文中,当我们使用具有不同 设置选项 的 Zlib 时,Conan 使用 package_id 来确保它获取与我们指定的配置匹配的正确二进制文件。