If you write a Rust library and you want to call it from C, you have to mark all the export functions with extern C and make sure everything uses the C ABI. But then you also have to write a header file by hand to include in your C code, and manually ensure that it is consistent with the Rust definitions, or Bad Things will happen. This seems both dangerous and tedious.
Is there a tool for generating C headers from Rust code? I've seen bindings that go the other way, for using C from Rust, and that's actually a harder problem!
A more recent way to do this is https://github.com/eqrion/cbindgen/, which seems to be actively maintained at the time of writing. The accepted answer from @hcs links to a GitHub issue that says that this should be tackled by an external tool.
It looks like this is a requested feature for Rust, but I haven't found any implementations.
UPDATE:
As @HameerAbbasi notes below, cbindgen is the preferred tool for doing this now.
It needs to be used so that I can iron out the kinks, but yes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With