cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Consider a libloading-based dynamic mode

Open dtolnay opened this issue 4 years ago • 1 comments

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

dtolnay avatar Jan 03 '21 08:01 dtolnay

It seems rust-bindgen has support for this. What would it take to add support for this here? Would love to contribute here.

samyak-jain avatar Jul 02 '21 17:07 samyak-jain