Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build script part to FFI chapter for more clear and smooth learn … #440

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ libc = "0.2.0"

[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

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