Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust cargo dylib clarification

I have a Rust project that I want to embed in another application as a dylib. I have crate_type = ["dylib"] set in my Cargo.toml, but I'm not sure if this should be cdylib instead.

When I build the project, it produces a libfoo.dylib in target/debug as expected, but looking at otool -L it also links against a libfoo.dylib in target/debug/deps/. If I'm going to be using this in another application, will I have to provide both dylibs?

Also, when the crate type is cdylib, it'll produce two dylibs as well but the one in deps will have a hash tacked onto the end of the filename. I'm not sure what the difference between these two crate types are.

I can avoid getting two dylibs by using just rustc, however I'd like to use cargo. What is the recommended way of going about embedding Rust as a dylib in other applications?

I'm using Rust on macOS 10.12 if that's significant

like image 924
jefftime Avatar asked May 24 '17 22:05

jefftime


1 Answers

After a bit more research, it turns out cdylib statically links the Rust dependencies, and dylib is for dynamic linking with other Rust projects.

As for embedding, everything I found suggests that statically linking the Rust library is the recommended way. For using the Rust library dynamically, setting rpath = true in my [profile] section solved the issues with redundant dylibs.

like image 180
jefftime Avatar answered Sep 19 '22 12:09

jefftime