OS X上的Rust和加载程序路径(@ rpath,@ loader_path) [英] Rust and loader paths (@rpath, @loader_path) on OS X

查看:426
本文介绍了OS X上的Rust和加载程序路径(@ rpath,@ loader_path)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决Rust加载国外库的问题.

I'm trying to solve a problem with foreign library loading with Rust.

输入:

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

I have an executable rtest and a dylib libcpp2rs.dylib. The library is linked to the executable through FFI:

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

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

My build.rs file (I'm passing an extra argument with libcpp2rs.dylib location):

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命令进行编译.

And I use cargo build command for compilation.

输出:

otool告诉我该库将由RPATH加载:

otool shows me that library will be loaded by 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节:

But at the same time there is no LC_LPATH section in executable:

> otool -l rtest | grep LC_RPATH
>

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

And it leads my application to a loading error:

> ./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来解决,但我希望不要在编译过程中引入其他步骤.

This issue can be fixed by install_name_tool usage, but I prefer do not introduce additional steps into compilation process.

  1. 是否可以(以及如何)使用cargo配置/构建脚本将加载类型从@rpath更改为@loader_path?
  2. 是否可以将@rpath值传递给cargo?
  1. Is it possible (and how) to change loading type from @rpath to @loader_path with cargo configurations/build script?
  2. Is it possible to pass @rpath value to cargo?

推荐答案

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

After some researches around I've found that the actual problem is 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.看起来应该像这样:

rustc uses a dylib ID as a reference for linkage type and if you'd like to change linkage type for a library to @loader_path for example, you have to fix dylib ID. It should looks like:

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

这篇关于OS X上的Rust和加载程序路径(@ rpath,@ loader_path)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆