在 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
查看上面的教程以理解所创建的文件。
除了这些文件,我们还将创建一个配置文件
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: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
我们现在可以验证配方和包二进制文件是否在缓存中
$ 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
的。
另请参阅