Build a C-callable library
Quick answer
A C-callable Rust library exports a deliberate set of symbols with compatible signatures and documented behavior. A small scalar-only interface is a good starting point before exposing pointers or callbacks.
Export and test a minimal library
In an empty Linux test directory, save the first example as lib.rs. In Rust 2024 unsafe(no_mangle) requires a unique, correctly defined symbol in the linked program. The function uses saturating addition so overflow never panics. The second example builds a cdylib and compiles a matching C client; other platforms have different filenames and linker options. A staticlib is another option but needs appropriate native linking flags. Never export Rust-owned allocation types without a matching ownership API.
// SAFETY: in the controlled example this is the only definition of bm_v140_sum_sat.#[unsafe(no_mangle)]pub extern "C" fn bm_v140_sum_sat(a: u32, b: u32) -> u32 { a.saturating_add(b)}Manual local Rust/Cargo candidate; not executed. Expected behavior:
Library source; no main and no standalone stdout.The function specifies both the calling convention and overflow policy.
rustc --edition=2024 --crate-type=cdylib lib.rs -o libbm_v140.socat > client.c <<'C'#include <stdint.h>#include <inttypes.h>#include <stdio.h>extern uint32_t bm_v140_sum_sat(uint32_t, uint32_t);int main(void) { printf("%" PRIu32 "\n", bm_v140_sum_sat(20, 22)); return 0;}Ccc client.c -L. -lbm_v140 -Wl,-rpath,'$ORIGIN' -o client./clientManual terminal step; not executed. Expected behavior (environment dependent):
42 if the compatible Linux toolchain builds and loads the library.The C declaration matches the fixed-width Rust signature. Review exported names and platform-specific linking before shipping.
Common mistake
Do not expose an unversioned Rust ABI or let symbol names collide with unrelated libraries.
Try it yourself
Add a C-side test that adds UINT32_MAX and 1 and verifies the documented saturated result.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build a C-callable library — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A C-callable arithmetic function must avoid overflow panic. Which API expresses saturating addition?
Build a C-callable library — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust // SAFETY: in the controlled example this is the only definition of bm_v140_sum_sat. #[unsafe(no_mangle)] pub extern "C" fn bm_v140_sum_sat(a: u32, b: u32) -> u32 { a.saturating_add(b) } ``` Which outcome or interpretation best matches this example?
Build a C-callable library — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash rustc --edition=2024 --crate-type=cdylib lib.rs -o libbm_v140.so cat > client.c <<'C' #include <stdint.h> #include <inttypes.h> #include <stdio.h> extern uint32_t bm_v140_sum_sat(uint32_t, uint32_t); int main(void) { printf("%" PRIu32 "\n", bm_v140_sum_sat(20, 22)); return 0; } C cc client.c -L. -lbm_v140 -Wl,-rpath,'$ORIGIN' -o client ./client ``` Which outcome or interpretation best matches this example?