wchar_t is always replaced with u16 #3029
-
Hi! I am trying to convert this simple sample header to a binding. I do not understand why A general question anyway -> How would you handle this type in a cross-platform way? I was thinking of using the widestring crate. Here is the header: void my_function(wchar_t const * path = nullptr); Here is the relevant part of my build.rs let bindings = bindgen::Builder::default()
.header("src/test.hpp")
// Configure bindgen to generate libloading bindings
.dynamic_library_name("test")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from("src");
bindings
.write_to_file(out_path.join("bindings_test.rs"))
.expect("Couldn't write bindings!"); And here is the bindings file /* automatically generated by rust-bindgen 0.70.1 */
pub struct test {
__library: ::libloading::Library,
pub my_function: Result<unsafe extern "C" fn(path: *const u16), ::libloading::Error>,
}
impl test {
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 my_function = __library.get(b"my_function\0").map(|sym| *sym);
Ok(test {
__library,
my_function,
})
}
pub unsafe fn my_function(&self, path: *const u16) {
(self
.my_function
.as_ref()
.expect("Expected function, got error."))(path)
}
} Thanks for your help, Christian |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I can't reproduce it in a standalone header via the CLI, e.g. I get a 32-bit size in Linux x86_64, unless I use
In C mode, I do get a |
Beta Was this translation helpful? Give feedback.
-
Hi @ckrenslehner, so I did a quick test with the header you provided void my_function(wchar_t const * path = nullptr); and /* automatically generated by rust-bindgen 0.70.1 */
extern "C" {
#[link_name = "\u{1}_Z11my_functionPKw"]
pub fn my_function(path: *const u32);
} even though the @ojeda is right, you can only get a typedef for it using C as in C++ it seems to be treated as just a builtin type. |
Beta Was this translation helpful? Give feedback.
Hi @ckrenslehner, so I did a quick test with the header you provided
and
wchar_t
is mapped tou32
in my host (x86_64-unknown-linux-gnu
) if I runbindgen input.hpp
:even though the
widestring
idea sounds interesting, historically we try to just generate the bindings for the desired target and the recommended approach here is to just generate bindings for each target individually and gate them usingcfg!
@ojeda is right, you can only get a typedef for it using C as in C++ it seems to be t…