Skip to content

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

Closed
@DrTobe

Description

@DrTobe

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions