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

Declaring extern "C" functions to be called from Rust #236

Closed
DrTobe opened this issue May 5, 2020 · 0 comments · Fixed by #315
Closed

Declaring extern "C" functions to be called from Rust #236

DrTobe opened this issue May 5, 2020 · 0 comments · Fixed by #315

Comments

@DrTobe
Copy link

DrTobe commented May 5, 2020

In chapter 9.1 "A little C with your Rust", the extern "C" function is declared with

pub extern "C" fn cool_function(
    i: cty::c_int,
    c: cty::c_char,
    cs: *mut CoolStruct
);

I was not able to successfully build my project with such a declaration, I always ended up having "free function without a body" errors. After having compared this approach with several other sources, it seems to me that the only correct way to declare this function would be

extern "C" {
    fn cool_function(
        i: cty::c_int,
        c: cty::c_char,
        cs: *mut CoolStruct
    );
}

The pub extern "C" fn ... syntax only seems to be valid to expose Rust's functions.

Please correct me if I'm wrong.

Furthermore, the #[link(name = "<library-name>")] could be prepended, possibly with a reference to further documentation.

bors bot added a commit that referenced this issue Apr 19, 2022
315: Correct C interop declarations r=adamgreig a=JVMerkle

Functions with external C linkage have to be wrapped with `extern
"C" { ... }`, `extern "C"` as part of the function signature is not
sufficient.

Resolves #236

Signed-off-by: Julian Merkle <[email protected]>

Co-authored-by: Julian Merkle <[email protected]>
@bors bors bot closed this as completed in #315 Apr 19, 2022
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.

1 participant