在 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

查看上面的教程以了解创建的文件。

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

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"}

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

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

win_bash = True

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

注意

没有必要,实际上在大多数情况下都不建议已经在 msys2 终端中运行。 Conan 将自动在定义的 bash shell 中运行 autotools 的构建子进程。

如果已经在 bash shell 中运行,则有必要激活 tools.microsoft.bash:activate=True conf。

让我们使用当前默认配置从源代码构建包,确保停用 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

的。

另请参阅