Excuse me, I want to call C++'s dynamic library from rust. But I don't want to build from rust.Like this,
cc::Build::new()
.file("src/foo.cc")
.shared_flag(true)
.compile("libfoo.so");
I don't want to compile in rust, just need to call c++ dynamic library (*.so).Because in some cases, I only need to call several functions, not all the functions need to call and use.How can I use it?
Zsolt
October 22, 2018, 6:51am
2
Here is my C example: https://github.com/hg2ecz/SWIG_examples/tree/master/compiled-C-and-Rust/Rust-shared-link
You don't need the Command::new("make").... row in build.rs, only the last 2 rows.
1 Like
Thank you for your example, but this is a little different from my needs. Because of my main usage, I want to be able to call the external C++ dynamic library (*.so). But still very grateful.
I don't think, that there is such use case covered by Rust by its own. What you need to know is to use dlfcn C interface to interoperate with so files (see: c - How can a shared library (.so) call a function that is implemented in its loader code? - Stack Overflow ). Using is directly would require much unsafe code, but I found this crate: GitHub - szymonwieloch/rust-dlopen: Rust library for opening and working with dynamic link libraries. which wraps it for you (but I never used it).
Thanks for you help!!
Maybe I need to refer to more information, still hope that someone else may use this case like me.
kornel
October 22, 2018, 1:02pm
6
To link with an existing library, make the build script output search path and library name:
cargo:rustc-link-lib=foo
cargo:rustc-link-search=native=/path/to/foo
https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script
If the library is a standard one, also check pkg-config crate and sys crates pattern .