Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extern "C" functions being re-exported #709

Closed
jasonish opened this issue Aug 20, 2021 · 4 comments · Fixed by #944
Closed

Extern "C" functions being re-exported #709

jasonish opened this issue Aug 20, 2021 · 4 comments · Fixed by #944

Comments

@jasonish
Copy link

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".

@emilio
Copy link
Collaborator

emilio commented Aug 29, 2021

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

@MatrixDev
Copy link

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

@jasonish
Copy link
Author

jasonish commented Dec 8, 2021

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.

@emilio
Copy link
Collaborator

emilio commented Apr 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants