Skip to content

Commit 3ff3843

Browse files
authored
Merge pull request #440 from RoggeOhta/master
Add build script part to FFI chapter for more clear and smooth learn …
2 parents 24cb15d + aad0fd9 commit 3ff3843

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/ffi.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ libc = "0.2.0"
2121

2222
[libc]: https://crates.io/crates/libc
2323

24+
## Prepare the build script
25+
26+
Because [snappy](https://github.com/google/snappy) is a static library by default.
27+
So there is no C++ std linked in the output artifact.
28+
n order to use this foreign library in Rust, we have to manually specify that we want to link stdc++ in our project.
29+
The easiest way to do this is by setting up a build script.
30+
31+
First edit `Cargo.toml`, inside `package` add `build = "build.rs"`:
32+
```toml
33+
[package]
34+
...
35+
build = "build.rs"
36+
```
37+
38+
Then create a new file at the root of your workspace, named `build.rs`:
39+
```rust
40+
// build.rs
41+
fn main() {
42+
println!("cargo:rustc-link-lib=dylib=stdc++"); // This line may be unnecessary for some environment.
43+
println!("cargo:rustc-link-search=<YOUR SNAPPY LIBRARY PATH>");
44+
}
45+
```
46+
47+
For more information, please read [The Cargo Book - build script](https://doc.rust-lang.org/cargo/reference/build-scripts.html).
48+
49+
2450
## Calling foreign functions
2551

2652
The following is a minimal example of calling a foreign function which will

0 commit comments

Comments
 (0)