Add build script part to FFI chapter for more clear and smooth learn experience

pull/440/head
RoggeOhta 10 months ago
parent 6bc2415218
commit 0b090c4ad2

@ -21,6 +21,29 @@ libc = "0.2.0"
[libc]: https://crates.io/crates/libc [libc]: https://crates.io/crates/libc
## Prepare the build script
Because [snappy](https://github.com/google/snappy) is a static library by default. So there is no C++ std linked in the output artifact. In order to use this foreign library in Rust, we have to manually specify that we want to link stdc++ in our project. The easiest way to do this is by setting up a build script.
First edit `Cargo.toml`, inside `package` add `build = "build.rs"`.
```toml
[package]
...
build = "build.rs"
```
Then create a new file at the root of your workspace, named `build.rs`.
```rust
// build.rs
fn main() {
    println!("cargo:rustc-link-lib=dylib=stdc++");
    println!("cargo:rustc-link-search=<YOUR SNAPPY LIBRARY PATH>");
}
```
For more information, please read [The Cargo Book - build script](https://doc.rust-lang.org/cargo/reference/build-scripts.html).
## Calling foreign functions ## Calling foreign functions
The following is a minimal example of calling a foreign function which will The following is a minimal example of calling a foreign function which will

Loading…
Cancel
Save