在 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 文件
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 系统的路径。
在包配方 conanfile.py 中,我们将有
win_bash = True
这非常重要,它告诉 Conan 在构建此包时,必须启动一个 bash shell 在其中执行构建。
注意
大多数情况下,不必(事实上也不推荐)已经在 msys2 终端中运行。Conan 将在定义的 bash shell 中自动运行 autotools 的构建子进程。
如果已经在 bash shell 中运行,则必须激活 tools.microsoft.bash:active=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
我们现在可以验证配方和包二进制文件是否在缓存中
$ 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 的。
另请参阅