Use C library with opaque pointers (and bindgen)

Hi. I'm writing a small tool calling into a C library. My original idea was to use bindgen to create the FFI code. The problem is that the library uses a lot of opaque pointers, something like this:

In the library header:

struct xxx;

void do_something_with_xxx(struct xxx *x);

In the C file:

struct xxx {
   ...
}

bindgen is not able to see the struct definition so it generates dummy structs instead.

What's the RUST-y way in this case? Should I just redefine each opaque struct directly in the RUST code?

There's nothing wrong with leaving opaque pointers opaque. The dummy struct that bindgen generates is not a problem; you should be using the C api to access it anyway.

1 Like

Yes if I want to exclusively use the library API (I mean, there is a reason why they are opaque after all :slight_smile:) I was actually thinking to add a layer or top of the library having access to the struct internals but on a second thought this is probably not a good idea.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.