1

I'm trying to port a C++ library to Rust. One dependency isn't open-source, and I only have access to the header files. rust-bindgen doesn't generate valid bindings from these header files, and after searching online for a while, I came to the conclusion that I needed to write a wrapper for the aforementioned library. (This header files for this library are from https://github.com/wpilibsuite/ni-libraries, if it matters).

Because of these issues, I can't include the original header files in the wrapper's header files, but I can include the original headers in the actual cpp files.

To use classes, I've just forward-declared them, but some of these headers use typedefs and other fields inside the class as return types. There doesn't seem to be a (good) way to forward-declare or otherwise use these.

I'd like to know if my approach is correct, or if there's a better way to do this and I'm going the wrong way about this (it could be either in C++ or with rust-bindgen itself).

An example of my current setup:

closed-source.h

class Foo {
    Foo* create();
    int getBar();
}

wrapper.hpp

class Foo;
Foo* foo_create();
int foo_getBar();

wrapper.cpp

#include "closed-source.h"
#include "wrapper.hpp"

Foo* foo_create() {
    return Foo::create();
}
int foo_getBar(Foo* self) {
    return self->getBar();
}
object-Object
  • 1,587
  • 1
  • 12
  • 14
  • Please tell me if you need more context – object-Object Oct 27 '21 at 01:36
  • You're probably better off not using the actual types at all. Either declare your own `struct MyFoo` or settle for a bunch of `void`. – o11c Oct 27 '21 at 01:40
  • @o11c I need some functions to return a pointer to the object – object-Object Oct 27 '21 at 01:50
  • The only thing you will be able to do with the returned pointer is to pass it back to some other function of the same library → return `void*` (or an alias) and have the other functions take a `void*`. – Jmb Oct 27 '21 at 06:50
  • I updated the example in the post, issue is I’m actually *using* the pointer by passing it back to the wrapper (saw this approach online so I assume it works) – object-Object Oct 27 '21 at 13:37
  • So? `int foo_getBar(void* self) { Foo* foo = self; return foo->getBar(); }` – Jmb Oct 28 '21 at 06:29

0 Answers0