首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当使用panic_abort选项交叉编译Rust程序时,如何消除对libunwind的依赖?

当使用panic_abort选项交叉编译Rust程序时,如何消除对libunwind的依赖?
EN

Stack Overflow用户
提问于 2022-09-05 20:25:33
回答 1查看 215关注 0票数 1

我在编译时指定了-Cpanic=abort-Zbuild-std=panic_abort。为什么链接器仍然说它需要libunwind来编译一个程序?

我正在尝试各种方法来交叉编译尽可能小的Rust程序(使用生锈回购作为参考)。现在,我正试图编译powerpc64-unknown-linux-musl目标,而我只能尝试删除对libunwind的依赖。

这是我的装置:

代码语言:javascript
复制
# 1. Install the Rust std source code
rustup component add rust-src --toolchain nightly
# 2. Setup a simple rust repo
cargo init testing
cd testing
# 3. Download a musl toolchain
wget https://musl.cc/powerpc64-linux-musl-cross.tgz
tar xzf powerpc64-linux-musl-cross.tgz
# 4. Try to compile the project (options on the command line instead of in files for
# maximum obviousness).
# RUSTFLAGS:
#  -Cpanic=abort - abort immediately on panic
#  -Clink-self-contained=no - don't use rustc's builtin libraries and objects (this
#    is needed because powerpc64-unknown-linux-musl is a tier 3 target)
#  -Clink-arg=--sysroot and -Clink-arg=/path/to/sysroot - pass the option to the linker
#    to specify the sysroot of cross-compilation toolchain
# Cargo options:
#  --config target.<triple>.linker - specify the linker to use
#  -Zbuild-std=std,panic_abort - build the standard library from source. Specify
#    panic_abort to make the abort on panic work
RUSTFLAGS="-Cpanic=abort -Clink-self-contained=no -Clink-arg=--sysroot -Clink-arg=powerpc64-linux-musl-cross/powerpc64-linux-musl/" \
cargo +nightly build \
    --config "target.powerpc64-unknown-linux-musl.linker=\"powerpc64-linux-musl-cross/bin/powerpc64-linux-musl-gcc\"" \
    --target powerpc64-unknown-linux-musl -Zbuild-std=panic_abort,std --release

如果出现以下错误,这将失败:

代码语言:javascript
复制
error: linking with `/home/user/Projects/testing/powerpc64-linux-musl-cross/bin/powerpc64-linux-musl-gcc` failed: exit status: 1

<output snipped>

  = note: /home/user/Projects/testing/powerpc64-linux-musl-cross/bin/../lib/gcc/powerpc64-linux-musl/11.2.1/../../../../powerpc64-linux-musl/bin/ld: cannot find -lunwind
EN

回答 1

Stack Overflow用户

发布于 2022-10-13 18:12:41

在最小大小的锈蚀存储库中:“即使Cargo.toml中指定了panic_immediate_abort = "abort”,默认情况下,rustc仍将在最终二进制文件中包含恐慌性字符串和格式化代码。

若要使用此功能,请重复上面的说明以使用build,但也可以传递以下-Z =features_abort选项。

但是,您将得到“无法找到-lunwind",因为链接器仍然使用libunwind,即使它确实不需要,为什么!我不知道,也许是个bug。(也许对链接器有一定了解的人可以轻松解决这个问题。我尝试了一个天真的解决方案,即“货物.--详细的”,复制,删除"libunwind“,然后重新连接失败)

我通过从源代码构建(- target =x86_64-未知-linux-musl)验证了这确实是缺少的部分,并使用了一个古老的简单技巧,即在目标lib文件夹中的“自包含”目录中的“触摸libunwin.a”。(因为链接器仍然会使用它,即使它现在确实是不必要的,然后我给了他一个虚拟libunwin.a)。

在你的例子中,我真的试着把它建立到你的目标上,直到我头疼,但我不能停下来,但以下是可能的解决方案:

如果您使用的是"-Z build -If =-If_ process _abort“,您可以自定义链接过程,然后解决它(直到问题解决为止) -Create一个虚拟的(空的) libunwind.a。a,它应该在您的工具链中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73614370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档