Skip to content

Commit 0d73660

Browse files
bors[bot]Julian Merkle
andauthored
Merge #315
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]>
2 parents 12f95ff + c1e7ca0 commit 0d73660

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/interoperability/c-with-rust.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ pub struct CoolStruct {
3737
pub y: cty::c_int,
3838
}
3939
40-
pub extern "C" fn cool_function(
41-
i: cty::c_int,
42-
c: cty::c_char,
43-
cs: *mut CoolStruct
44-
);
40+
extern "C" {
41+
pub fn cool_function(
42+
i: cty::c_int,
43+
c: cty::c_char,
44+
cs: *mut CoolStruct
45+
);
46+
}
4547
```
4648

4749
Let's take a look at this definition one piece at a time, to explain each of the parts.
@@ -61,7 +63,7 @@ pub y: cty::c_int,
6163
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.
6264

6365
```rust,ignore
64-
pub extern "C" fn cool_function( ... );
66+
extern "C" { pub fn cool_function( ... ); }
6567
```
6668

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

0 commit comments

Comments
 (0)