cxx
cxx copied to clipboard
Consider a libloading-based dynamic mode
Imagining something like:
#[cxx::bridge]
mod ffi {
#[dynamic_load]
extern "C++" {
/* as normal */
fn demo(s: &str) -> u8;
}
}
fn main() -> Result<()> {
let ffi = ffi::load("/path/to/liblibrary.so")?;
let x: u8 = ffi.demo?("...");
println!("{}", x);
Ok(())
}
where loading produces an appropriate wrapper struct around libloading::Library. Importantly, not all of the symbols may be available in the runtime version of the library, hence the ? when calling one.
Related discussions:
- https://github.com/rust-lang/rust-bindgen/issues/1541
- https://github.com/rust-lang/rust-bindgen/pull/1846
- https://github.com/rust-lang/rust-bindgen/commit/fc5fa9a8f2847b3d53ab88b80dd86165ea4da29b
It seems rust-bindgen has support for this. What would it take to add support for this here? Would love to contribute here.