【问题标题】:Rust and loader paths (@rpath, @loader_path) on OS XOS X 上的 Rust 和加载程序路径(@rpath、@loader_path)
【发布时间】:2016-05-05 11:05:19
【问题描述】:

我正在尝试解决使用 Rust 加载外部库的问题。

输入:

我有一个可执行文件 rtest 和一个 dylib libcpp2rs.dylib。该库通过 FFI 链接到可执行文件:

#[link(name="cpp2rs")]
extern { ... }

我的build.rs 文件(我正在传递一个带有libcpp2rs.dylib 位置的额外参数):

pub fn main() {
    println!("cargo:rustc-link-search=native=./cpplib/bin");
}

还有我的Cargo.toml 文件:

[package]
name = "rtest"
version = "0.1.0"
authors = ["astavonin"]
build = "build.rs"
rpath = true
[dependencies]
libc = "0.2.10"

我使用cargo build 命令进行编译。

输出:

otool 显示库将由RPATH 加载:

> otool -L rtest
rtest:
@rpath/libcpp2rs.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

但同时可执行文件中没有LC_LPATH部分:

> otool -l rtest | grep LC_RPATH
>

它会导致我的应用程序出现加载错误:

> ./rtest 
dyld: Library not loaded: @rpath/libcpp2rs.dylib
  Referenced from: /Users/astavonin/projects/Tests/rtest/target/debug/./rtest
  Reason: image not found
zsh: trace trap  ./rtest

这个问题可以通过install_name_tool 的使用来解决,但我不希望在编译过程中引入额外的步骤。

  1. 是否可以(以及如何)使用 cargo 配置/构建脚本将加载类型从 @rpath 更改为 @loader_path?
  2. 是否可以将@rpath 值传递给cargo?

【问题讨论】:

  • 请edit 在您的问题中包含libcpp2rs.dylib 的位置、您如何告知Rust 它在哪里以及您是如何构建代码的。

标签: macos rust rust-cargo rpath


【解决方案1】:

经过一番研究,我发现实际问题是libcpp2rs.dylib ID:

> otool -L cpplib/bin/libcpp2rs.dylib 
cpplib/bin/libcpp2rs.dylib:
    @rpath/libcpp2rs.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

rustc 使用 dylib ID 作为链接类型的参考,例如,如果您想将库的链接类型更改为 @loader_path,则必须修复 dylib ID。它应该看起来像:

@loader_path/libcpp2rs.dylib (compatibility version 0.0.0, current version 0.0.0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2013-05-25
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多