Cbindgen: omitting prototypes for extern "C" blocks

Given a simple module

extern "C" {
    fn memcpy(dest: *mut u8, src: *const u8, len: usize);
}

#[no_mangle]
pub extern "C" fn add(a: u32, b: u32) -> u32 {
    a+b
}

cbindgen produces the following C header:

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

extern void memcpy(uint8_t *dest, const uint8_t *src, uintptr_t len);

uint32_t add(uint32_t a, uint32_t b);

Is there a way to get cbindgen not to emit extern declarations? (I could use the existing exclude functionality to name specific functions to exclude, but I don't want to have to update that list as I add more declarations.)

Not with current cbindgen it seems:

Seems like a reasonable feature request or basis for a PR.

1 Like

You could use safer-ffi's #[ffi_export]:

use ::safer_ffi::prelude::*;

extern "C" {
    fn memcpy(dest: *mut u8, src: *const u8, len: usize);
}

#[ffi_export]
pub fn add(a: u32, b: u32) -> u32 {
    a+b
}

See the guide

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.