Skip to content

Commit

Permalink
Merge #315
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bors[bot] and Julian Merkle authored Apr 19, 2022
2 parents 12f95ff + c1e7ca0 commit 0d73660
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/interoperability/c-with-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ pub struct CoolStruct {
pub y: cty::c_int,
}
pub extern "C" fn cool_function(
i: cty::c_int,
c: cty::c_char,
cs: *mut CoolStruct
);
extern "C" {
pub fn cool_function(
i: cty::c_int,
c: cty::c_char,
cs: *mut CoolStruct
);
}
```

Let's take a look at this definition one piece at a time, to explain each of the parts.
Expand All @@ -61,7 +63,7 @@ pub y: cty::c_int,
Due to the flexibility of how C or C++ defines an `int` or `char`, it is recommended to use primitive data types defined in `cty`, which will map types from C to types in Rust.

```rust,ignore
pub extern "C" fn cool_function( ... );
extern "C" { pub fn cool_function( ... ); }
```

This statement defines the signature of a function that uses the C ABI, called `cool_function`. By defining the signature without defining the body of the function, the definition of this function will need to be provided elsewhere, or linked into the final library or binary from a static library.
Expand Down

0 comments on commit 0d73660

Please sign in to comment.