cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

Extern "C" functions being re-exported

Open jasonish opened this issue 3 years ago • 3 comments

We have an application that is part Rust, part C. We use extern "C" { to define C functions to Rust. However, cbindgen appears to re-export these back out. Short of listing all these in the exclude list, can they still be ignored?

We still want to export Rust functions that are declared extern so our C code can bind to them.

Note: When just extern { is used these C functions are not re-exported, but we'd like to use as many rustfmt defaults as possible, which rewrites these to extern "C".

jasonish avatar Aug 20 '21 15:08 jasonish

Can you annotate them on the source with /// cbindgen:ignore?

emilio avatar Aug 29 '21 11:08 emilio

Comment /// cbindgen:ignore actually works but it would be nice to ignore those by default.

MatrixDev avatar Oct 20 '21 11:10 MatrixDev

Can you annotate them on the source with /// cbindgen:ignore?

This works on the module level, but not just on an extern "C" {} block which would be more useful.

I think the bigger question is why extern "C" {} and extern {} are treated differently by cbindgen? And why export functions defined this way anyways, presumably they're in a C header already. Our use case is to create C bindings for a C app to use our Rust code. We don't need cbindgen to re-export definitions of extern functions.

Happy to look at a fix if there is not a specific reason for the current behaviour.

jasonish avatar Dec 08 '21 18:12 jasonish

I think this works now.

extern "C" {
    fn foo();
}

/// cbindgen:ignore
extern "C" {
    fn bar();
}

Generates only foo. As for why extern {} and extern "C" {} are different, that's cbindgen being overly restrictive.

emilio avatar Apr 15 '24 01:04 emilio