rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

--dynamic-loading ignores static variables

Open pradt2 opened this issue 4 years ago • 0 comments

Hi, it seems to me that the --dynamic-loading feature doesn't support static variables. Is this expected? If so, why? If not, would someone be willing to take a look/mentor my efforts?

Input C/C++ Header

typedef char (*myFoo)();
static myFoo myFooVar;

char myBar();

Bindgen Invocation

$ bindgen --allowlist-function "my.*" --allowlist-type "my.*" --allowlist-var "my.*" --no-layout-tests --dynamic-loading mylib input.h > output.rs

Actual Results

The output is the same for both current 0.59.1 and today's master branch.

/* automatically generated by rust-bindgen 0.59.1 */

pub type myFoo = ::std::option::Option<unsafe extern "C" fn() -> ::std::os::raw::c_char>;
extern "C" {
    pub static mut myFooVar: myFoo;
}
extern crate libloading;
pub struct mylib {
    __library: ::libloading::Library,
    pub myBar: Result<unsafe extern "C" fn() -> ::std::os::raw::c_char, ::libloading::Error>,
}
impl mylib {
    pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
    where
        P: AsRef<::std::ffi::OsStr>,
    {
        let library = ::libloading::Library::new(path)?;
        Self::from_library(library)
    }
    pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
    where
        L: Into<::libloading::Library>,
    {
        let __library = library.into();
        let myBar = __library.get(b"myBar\0").map(|sym| *sym);
        Ok(mylib { __library, myBar })
    }
    pub unsafe fn myBar(&self) -> ::std::os::raw::c_char {
        (self.myBar.as_ref().expect("Expected function, got error."))()
    }
}

Expected Results

I would expect bindgen to include myFuncVar in the mylib struct and use libloading to dynamically load it in the from_library function (the same way it does for myBar).

pradt2 avatar Nov 03 '21 12:11 pradt2