rust-bindgen
rust-bindgen copied to clipboard
Is there a way to gen func type from c/cpp func declaration
Input C/C++ Header
extern void add(int a, int b);
now i got:
extern "C" {
pub fn add(a: i32, b:i32);
}
i also want:
type Tadd = unsafe extern "C" fn(i32,i32);
You can manually add a typedef
.
header.h
void add(int, int);
typedef typeof(add) TAdd;
Gives:
/* automatically generated by rust-bindgen 0.69.1 */
extern "C" {
pub fn add(arg1: ::std::os::raw::c_int, arg2: ::std::os::raw::c_int);
}
pub type TAdd = ::std::option::Option<
unsafe extern "C" fn(arg1: ::std::os::raw::c_int, arg2: ::std::os::raw::c_int),
>;
Using typeof
is fine, since rust-bindgen
uses clang, and clang supports typeof
.
If you don't want to add the typedef
s manually to your headers, you can add them using the header_contents
method.
This has been requested before: https://github.com/rust-lang/rust-bindgen/discussions/2608
@Frost-54 It looks no different than writing directly in rust. I still want it to be automatic.