I'm trying to solve a problem with foreign library loading with Rust.
Inputs:
I have an executable rtest
and a dylib libcpp2rs.dylib
. The library is linked to the executable through FFI:
#[link(name="cpp2rs")]
extern { ... }
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");
}
And my Cargo.toml
file:
[package]
name = "rtest"
version = "0.1.0"
authors = ["astavonin"]
build = "build.rs"
rpath = true
[dependencies]
libc = "0.2.10"
And I use cargo build
command for compilation.
Outputs:
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)
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
This issue can be fixed by install_name_tool
usage, but I prefer do not introduce additional steps into compilation process.
- Is it possible (and how) to change loading type from @rpath to @loader_path with
cargo
configurations/build script? - Is it possible to pass @rpath value to
cargo
?