From 6be1fec00c38378d8696e1a1561201aa0ea253c2 Mon Sep 17 00:00:00 2001 From: RoggeOhta Date: Tue, 16 Jan 2024 23:16:24 +0800 Subject: [PATCH 1/3] Add build script part to FFI chapter for more clear and smooth learn experience --- src/ffi.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/ffi.md b/src/ffi.md index b4fdc02..9f174d4 100644 --- a/src/ffi.md +++ b/src/ffi.md @@ -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="); +} +``` + +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 From ed039e48fe661fa996138a6f746c54fd8ab602b4 Mon Sep 17 00:00:00 2001 From: RoggeOhta Date: Tue, 16 Jan 2024 23:25:29 +0800 Subject: [PATCH 2/3] Fix space character problem --- src/ffi.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ffi.md b/src/ffi.md index 9f174d4..7b08fce 100644 --- a/src/ffi.md +++ b/src/ffi.md @@ -36,8 +36,8 @@ 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="); + println!("cargo:rustc-link-lib=dylib=stdc++"); + println!("cargo:rustc-link-search="); } ``` From aad0fd90464a6b38f14d4520a4cd7840b01a382b Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sat, 5 Jul 2025 16:33:05 +0900 Subject: [PATCH 3/3] Apply suggestions from code review --- src/ffi.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ffi.md b/src/ffi.md index 7b08fce..2f065a4 100644 --- a/src/ffi.md +++ b/src/ffi.md @@ -23,20 +23,23 @@ libc = "0.2.0" ## 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. +Because [snappy](https://github.com/google/snappy) is a static library by default. +So there is no C++ std linked in the output artifact. +n 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"`. +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`. +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-lib=dylib=stdc++"); // This line may be unnecessary for some environment. println!("cargo:rustc-link-search="); } ```