将boost编译为通用库Intel和Apple Silicon体系结构

问题描述

我正在尝试在MacOS上将dylib创建为dylib。我需要同时为英特尔架构和即将推出的Apple Silicon(arm64)架构构建它。

我下载了boost并运行了以下命令:

./bootstrap.sh
./b2 -address-model=64 architecture=combined -a

lipo -archs始终显示生成的dylibs架构是x86_64

我有Xcode12 beta和MacOS Catalina 10.15.7,

如果我在Xcode中创建一个项目并在构建设置中设置archs arm64 x86_64,则可以构建一个示例通用库。

运行命令./b2 cxxflags="-arch arm64 -arch x86_64"失败,并出现以下错误:

    "clang++" -x c++ -fvisibility-inlines-hidden -m64 -O3 -Wall -fvisibility=hidden -Wno-inline -arch arm64 -arch x86_64 -ftemplate-depth-255 -fvisibility=hidden -fvisibility-inlines-hidden -DBOOST_ALL_NO_LIB=1 -DNDEBUG -I"." -c -o "bin.v2/libs/serialization/build/clang-darwin-12.0/release/link-static/threading-multi/visibility-hidden/polymorphic_xml_iarchive.o" "libs/serialization/src/polymorphic_xml_iarchive.cpp"

...failed clang-darwin.compile.c++ bin.v2/libs/serialization/build/clang-darwin-12.0/release/link-static/threading-multi/visibility-hidden/polymorphic_xml_iarchive.o...
clang-darwin.compile.c++ bin.v2/libs/serialization/build/clang-darwin-12.0/release/link-static/threading-multi/visibility-hidden/polymorphic_xml_oarchive.o
In file included from libs/serialization/src/polymorphic_xml_oarchive.cpp:16:
In file included from ./boost/serialization/config.hpp:18:
In file included from ./boost/config.hpp:57:
In file included from ./boost/config/platform/macos.hpp:28:
In file included from ./boost/config/detail/posix_features.hpp:18:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/unistd.h:71:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_types.h:27:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:32:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/cdefs.h:807:2: error: Unsupported architecture
#error Unsupported architecture
 ^
In file included from libs/serialization/src/polymorphic_xml_oarchive.cpp:16:
In file included from ./boost/serialization/config.hpp:18:
In file included from ./boost/config.hpp:57:
In file included from ./boost/config/platform/macos.hpp:28:
In file included from ./boost/config/detail/posix_features.hpp:18:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/unistd.h:71:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_types.h:27:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:33:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/machine/_types.h:34:2: error: architecture not supported
#error architecture not supported
 ^
In file included from libs/serialization/src/polymorphic_xml_oarchive.cpp:16:
In file included from ./boost/serialization/config.hpp:18:
In file included from ./boost/config.hpp:57:
In file included from ./boost/config/platform/macos.hpp:28:
In file included from ./boost/config/detail/posix_features.hpp:18:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/unistd.h:71:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_types.h:27:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:55:9: error: unknown type name '__int64_t'
typedef __int64_t       __darwin_blkcnt_t;      /* total blocks */
        ^
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:56:9: error: unknown type name '__int32_t'; did you mean '__int128_t'?
typedef __int32_t       __darwin_blksize_t;     /* preferred block size */
        ^
note: '__int128_t' declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:57:9: error: unknown type name '__int32_t'; did you mean '__int128_t'?
typedef __int32_t       __darwin_dev_t;         /* dev_t */
        ^
note: '__int128_t' declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:60:9: error: unknown type name '__uint32_t'; did you mean '__uint128_t'?
typedef __uint32_t      __darwin_gid_t;         /* [???] process and group IDs */
        ^
note: '__uint128_t' declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:61:9: error: unknown type name '__uint32_t'; did you mean '__uint128_t'?
typedef __uint32_t      __darwin_id_t;          /* [XSI] pid_t,uid_t,or gid_t*/
        ^

解决方法

以下内容适用于使用Xcode 12.2的Big Sur。在Catalina 10.15.7(19H15)上,我收到与OP相同的错误消息。

./bootstrap.sh --with-libraries=regex,date_time 
./b2 architecture=combined cxxflags="-arch x86_64 -arch arm64"
,

我也遇到了同样的问题,并遇到了这个answer,它是为i386和x86_64创建通用二进制文件。

要总结答案,您需要运行./b2两次(使用不同的工具集,因为clang始终为x86_64构建,即使您传递了适当的CXXFlags

这是我用来首先单独生成所需库,然后使用lipo

组合它们的脚本
#!/bin/sh

rm -rf arm64 x86_64 universal
./bootstrap.sh --with-toolset=clang --with-libraries=thread,system,filesystem,program_options,serialization 
./b2  cxxflags="-arch arm64" toolset=darwin -a
mkdir -p arm64 && cp stage/lib/*.dylib arm64
./b2 toolset=clang cxxflags="-arch arm64 -arch x86_64" -a
mkdir x86_64 && cp stage/lib/*.dylib x86_64
mkdir universal
for dylib in arm64/*; do 
  lipo -create -arch arm64 $dylib -arch x86_64 x86_64/$(basename $dylib) -output universal/$(basename $dylib); 
done
,

您不需要使用脂质,可以通过 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk到cxxflags,它将立即建立通用库。

,

我遵循了 documentation 的答案,这似乎是使用 Xcode 12.3 在 macOS 10.15.7 上的方法。

我唯一的问题是构建系统添加了额外的 -arch armv4t clang 选项(或类似的东西),这导致构建失败。使用 architecture=combine 的效果不如 Navan Chauhan。对我有用的是:

  1. bootstrap.sh --with-toolset=clang-darwincxxflagscflagslinkflags 设置为 -arch x86_64 -arch arm64 以及其他选项。
  2. x86_64 构建:b2 toolset=clang-darwin target-os=darwin architecture=x86 stagecxxflagscflagslinkflags 仅设置为 -arch x86_64(以及其他选项)。
  3. arm64 构建:b2 toolset=clang-darwin target-os=darwin architecture=arm abi=aapcs stagecxxflagscflagslinkflags 仅设置为 -arch arm64(以及其他选项)。
  4. 将库与 lipo... 合并

我也被迫设置 ABI,因为 automatic-guess-something 无法识别 Apple Silicon 的目标,因此某些组装内容未编译 - 因此 缺少符号 稍后在构建期间。最后,armv4t 问题由 clang-darwin 工具集解决。

目前唯一的警告是两个构建都进入相同的架构/配置目录,这可以通过自定义 user-config.jam 或在每个 {{1} 之后立即调用 install 来解决}} 这就是我所做的。

适用于:

  • 主机平台:英特尔
  • macOS 10.15.7
  • Xcode 12.3(安装了命令行工具)
  • Boost 1.75.0(依赖压缩和 ICU 库已经编译为通用)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...