-
Notifications
You must be signed in to change notification settings - Fork 707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generating bindings for headers using STL types #598
Comments
Thanks! I'll try a different version and see if I can get it working. Where would be the best place to ask about best practices? For instance, if I had
And set
|
Another issue I came across involves virtual functions: // cpp/Test.hpp
class Test {
public:
virtual void do_stuff();
}; with an out-of-line default implementation: // cpp/Test.cpp
#include "Test.hpp"
#include <iostream>
void Test::do_stuff() {
std::cout << "do stuff!" << std::endl;
} Unfortunately this doesn't generate a binding. I was able to work around it with a wrapper: void test_do_stuff(Test t) { t.do_stuff(); } I was also able to add this function manually: extern "C" {
#[link_name = "_ZN4Test8do_stuffEv"]
pub fn Test_do_stuff(t: &mut bindings::Test);
}
impl bindings::Test {
pub unsafe fn do_stuff(&mut self) {
Test_do_stuff(&mut *self)
}
} And that works: #[test]
fn test_do_stuff() {
unsafe {
let mut x = bindings::Test::new();
x.do_stuff()
}
} |
That wouldn't do the right thing if any other instance would override the virtual function. The proper thing to do is to fill the VTable with method pointers, and generate methods calling through them. Nobody has got to do it yet though, and vtable layout is kinda hard and not interoperable between MSVC and LLVM/gcc last time I checked, so it's not a trivial problem to solve. |
Regarding
The reply is you would need your own wrappers, at least for now. String inherits from basic_string<char_t> in all the libstd's I've known, and that's kinda hard to deal with, because we don't generate methods for template structs (they'd have different symbols depending on the instantiation, etc). |
Thanks for clarifying! Another question, if I may. Is this a sane thing to do? mem::transmute::<*mut Derived, *mut Base>(&mut value) Otherwise I'll need more wrappers: Base* derived_to_base(Derived* x) {
return static_cast<Base*>(x);
} |
You should be able to just use |
Thanks! Didn't know that's the way to do it. |
On master with libclang 3.9, invoking bindgen like this:
then I get this:
|
Which, although very verbose, seems correct to me. I suspect this got fixed in one of the recent related PRs. |
Going to go ahead and close this issue; if I'm overlooking anything, please reopen :) |
@fitzgen I ran into this issue via google. For me the problem is that bindgen's generated code creates some symbols with /* automatically generated by rust-bindgen */
#[repr(C)]
pub struct std___cxx11_basic_string<_CharT> {
pub _M_dataplus: std_basic_string__Alloc_hider,
pub _M_string_length: std_basic_string_size_type,
pub __bindgen_anon_1: std_basic_string__bindgen_ty_2<_CharT>,
}
pub type std___cxx11_basic_string__Char_alloc_type = __gnu_cxx___alloc_traits;
pub type std___cxx11_basic_string__Alloc_traits = __gnu_cxx___alloc_traits;
pub type std___cxx11_basic_string_traits_type<_Traits> = _Traits;
pub type std___cxx11_basic_string_value_type = [u8; 0usize];
pub type std___cxx11_basic_string_allocator_type =
std_basic_string__Char_alloc_type; // <== Note the missing cxx11 here!
... Since you wrote that this code looks correct to you, I wonder if I am missing something obvious here? I asked about this on IRC as well where it was pointed out that this might be an "ABI tagging" issue? |
@cbourjau I didn't dig into the generated code, just squinted at it, in that comment. Was mostly comparing against what we did before, which was panic. Would you mind filing a new issue with a reduced header and steps to reproduce and all that? |
Can I use it Without wrappers now? |
I'm trying to generate bindings for a header that uses STL types:
README states:
which makes it sound like it should work. My
bindgen
configuration is:However,
bindgen
fails with:Could you clarify whether this should work?
The text was updated successfully, but these errors were encountered: