rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Is there a way to gen func type from c/cpp func declaration

Open kirito41dd opened this issue 1 year ago • 4 comments

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);

kirito41dd avatar Jan 02 '24 07:01 kirito41dd

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.

Frost-54 avatar Jan 02 '24 08:01 Frost-54

If you don't want to add the typedefs manually to your headers, you can add them using the header_contents method.

pvdrz avatar Jan 02 '24 14:01 pvdrz

This has been requested before: https://github.com/rust-lang/rust-bindgen/discussions/2608

torokati44 avatar Jan 03 '24 00:01 torokati44

@Frost-54 It looks no different than writing directly in rust. I still want it to be automatic.

kirito41dd avatar Jan 05 '24 11:01 kirito41dd