在 Windows (msys2) 中使用 Autotools 创建你的第一个 Conan 包

警告

本示例适用于 Windows 操作系统,使用 msys2 子系统运行 autotools 构建系统。支持是有限的AutotoolsDeps 生成器在 Windows 上仍然无法工作,因此默认模板中的 test_package 将会失败。

请注意,本示例是使用 MSVC 编译器构建的,而不是 MinGW/gcc。即使构建系统是 autotools,本示例的目标是 MSVC 编译器,生成的包将是二进制兼容的,并且可以被使用 MSVC 和其他构建系统的其他包使用。没有必要强制使用 MinGW/gcc 来使用一些依赖 autotools 的开源依赖项,ConanCenter 使用 MSVC 构建所有这些依赖项。

使用 Autotools 创建你的第一个 Conan 包 教程中,介绍了 autotools 集成。请先阅读该部分以理解它们,因为本节只会介绍 Windows/msys2 特有的问题。

我们将使用同样的 conan new 命令创建一个“Hello World” C++ 库示例项目

$ conan new autotools_lib -d name=mypkg -d version=0.1

请查看上述教程以理解创建的文件。

除了这些文件,我们还将创建一个 profile 文件

msys2_profile
include(default)

[conf]
tools.microsoft.bash:subsystem=msys2
tools.microsoft.bash:path=C:\ws\msys64\usr\bin\bash
# since Conan 2.9, this "cl" compiler definition is not necessary
# by default for the 'compiler=msvc'
# tools.build:compiler_executables={"c": "cl", "cpp": "cl"}

请注意,你可能需要调整 msys2 系统的 bash 路径。

在包 recipe 文件 conanfile.py 中,我们将有

win_bash = True

这一点非常重要,它告诉 Conan 当需要构建这个包时,它必须启动一个 bash shell 并在其中执行构建。

注意

没有必要,并且在大多数情况下也不推荐已经在 msys2 终端内部运行。Conan 将自动在定义的 bash shell 中运行 autotools 的构建子进程。

如果已经在 bash shell 中运行,则需要激活 tools.microsoft.bash:activate=True 配置。

让我们使用当前的默认配置从源代码构建包,确保停用 test_package,否则它将会失败。

# Deactivating the test_package, as AutotoolsDeps doesn't work yet.
$ conan create . -pr=msys2_profile -tf=""

...
mypkg/0.1: package(): Packaged 1 '.h' file: mypkg.h
mypkg/0.1: package(): Packaged 1 '.la' file: libmypkg.la
mypkg/0.1: package(): Packaged 1 '.lib' file: mypkg.lib
mypkg/0.1: Created package revision fa661758835cf6f7f311c857447393cc
mypkg/0.1: Package '9bdee485ef71c14ac5f8a657202632bdb8b4482b' created

我们现在可以验证 recipe 和包二进制文件是否在缓存中

$  conan list "mypkg:*"
Found 1 pkg/version recipes matching mypkg in local cache
Local Cache
  mypkg
    mypkg/0.1
      revisions
        6e85b0c27c7fbc8eddc1994dbb543b52 (2024-04-30 18:29:44 UTC)
          packages
            9bdee485ef71c14ac5f8a657202632bdb8b4482b
              info
                settings
                  arch: x86_64
                  build_type: Release
                  compiler: msvc
                  compiler.cppstd: 14
                  compiler.runtime: dynamic
                  compiler.runtime_type: Release
                  compiler.version: 193
                  os: Windows
                options
                  shared: False

注意二进制文件是如何 compiler=msvc 的。

另请参阅