Native linking
Quick answer
Native linking resolves foreign symbols against libraries for a specific target. A matching declaration is essential: a linker can resolve a symbol even when its declared parameter types are wrong.
Build a controlled C fixture
This manual exercise uses a Linux-like environment with cc, ar and Rust 1.85.0, in an empty disposable directory. First create the C library using the terminal commands, then save the Rust example as main.rs and compile with rustc --edition=2024 main.rs -L native=. -o demo. Cargo projects can instead use build.rs to print cargo::rustc-link-search and cargo::rustc-link-lib instructions. Check the target, compiler, archive format and ABI; tools like bindgen can generate declarations but cannot certify ownership or safety.
cat > bm_add.c <<'C'#include <stdint.h>uint32_t bm_add(uint32_t a, uint32_t b) { return a + b; }Ccc -c bm_add.c -o bm_add.oar rcs libbm_add.a bm_add.oManual terminal step; not executed. Expected behavior (environment dependent):
Local object and static archive created if the tools and target are compatible.The implementation uses unsigned fixed-width values and no pointer arguments.
// SAFETY: declaration and link name match the controlled C fixture above.#[link(name = "bm_add", kind = "static")]unsafe extern "C" { fn bm_add(a: u32, b: u32) -> u32; }fn main() { // SAFETY: the matched function accepts these u32 arguments; no pointers. println!("{}", unsafe { bm_add(3, 5) });}Manual local Rust/Cargo candidate; not executed. Expected behavior:
After successful linking, ./demo prints 8.Run only with the exact local C source compiled above; other libraries need their own contract review.
Common mistake
Do not correct a missing or incompatible library by guessing its foreign function signature.
Try it yourself
Explain the different roles of the header, archive and Rust extern declaration.
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
Native linking — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A linker resolves a foreign symbol. Does this prove the declared Rust argument types match C?
Native linking — 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. ```bash cat > bm_add.c <<'C' #include <stdint.h> uint32_t bm_add(uint32_t a, uint32_t b) { return a + b; } C cc -c bm_add.c -o bm_add.o ar rcs libbm_add.a bm_add.o ``` Which outcome or interpretation best matches this example?
Native linking — 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. ```rust // SAFETY: declaration and link name match the controlled C fixture above. #[link(name = "bm_add", kind = "static")] unsafe extern "C" { fn bm_add(a: u32, b: u32) -> u32; } fn main() { // SAFETY: the matched function accepts these u32 arguments; no pointers. println!("{}", unsafe { bm_add(3, 5) }); } ``` Which outcome or interpretation best matches this example?