I would like to write a library to make it easier to write DLL plugins for a non-rust program, with a C API/ABI, but I do not see how I could achieve that.
In my poc I have functions like
#[no_mangle] (repeated each time)
extern "C" fn init(*mut *mut ctx) -> i32 { ... }
extern "C" fn do_something(*mut ctx) -> i32 { ... }
extern "C" fn cleanup(*mut ctx) -> i32 { ... }
I used a Box
ed structure for my ctx
variable.
I have a lot of boilerplate code in my PoC for these and would like to put all that in a library to avoid repeating it everywhere.
So basically my objective is to have a lib
that lets me just define the custom context and functions for each plugin, a bit like:
use plugin_lib;
struct MyCtx {
...
}
fn my_init(pp_ctx: *mut *mut MyCtx) -> i32 { ... };
fn my_init(p_ctx: *mut MyCtx) -> i32 { ... };
fn my_init(p_ctx: *mut MyCtx) -> i32 { ... };
But if I try to create a library with missing my_
functions, it will obviously not link, right? And at the same time if I just define objects in a lib and try to use them in the plugin part I cannot make it export functions unless I write all of them again (or at least small wrappers to call the lib ones, which sounds ugly).
What would be the right way to build this? Or if there are several, how would you do it?