Calling C functions from Rust with FFI and the cc crate

Rust’s foreign function interface (FFI) lets Rust call functions that follow the C ABI, which covers C libraries and anything else that exports C-compatible symbols. This post compiles a small C file as part of a Cargo build, then calls two functions from it: one that adds two integers and one that takes a string.
Project layout
Section titled “Project layout”cargo new ffi_democd ffi_democargo add --build ccffi_demo/├── Cargo.toml├── build.rs└── src/ ├── main.rs └── sum.ccargo add --build cc adds the cc crate under [build-dependencies]:
[build-dependencies]cc = "1"The C code
Section titled “The C code”src/sum.c:
#include <stddef.h>
int sum(int x, int y) { return x + y;}
size_t count_vowels(const char *s) { size_t n = 0; for (; *s != '\0'; s++) { switch (*s) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': n++; } } return n;}Building the C code with Cargo
Section titled “Building the C code with Cargo”build.rs runs before your crate is compiled. cc::Build finds the platform’s C compiler (gcc, clang or MSVC), compiles the file into a static library, and tells Cargo to link it:
fn main() { println!("cargo:rerun-if-changed=src/sum.c"); cc::Build::new().file("src/sum.c").compile("sum");}Because the library is linked statically, there’s no .so/.dll to install or find at runtime, and the same setup works on Linux, macOS and Windows. You still need a C compiler installed.
Declaring and calling the functions
Section titled “Declaring and calling the functions”src/main.rs:
use std::ffi::{c_char, c_int, CString};
unsafe extern "C" { fn sum(x: c_int, y: c_int) -> c_int; fn count_vowels(s: *const c_char) -> usize;}
fn main() { let x = 1; let y = 2; let result = unsafe { sum(x, y) }; println!("The sum of {x} and {y} is {result}.");
let text = CString::new("Hello from Rust").expect("no interior NUL bytes"); let vowels = unsafe { count_vowels(text.as_ptr()) }; println!("{vowels} vowels");}cargo runThe sum of 1 and 2 is 3.4 vowelsWhat the pieces mean
Section titled “What the pieces mean”extern "C"declares functions that use the C calling convention. The Rust compiler can’t see their bodies, so it trusts that the signatures you write match the C ones. A mismatch (say,i64where C hasint) compiles fine and produces garbage or crashes at runtime.unsafe extern: since the 2024 edition (the default for newcargo newprojects), extern blocks must be markedunsafe, as a reminder that the declarations themselves are a promise you’re making. On the 2021 edition, plainextern "C" { ... }still compiles.unsafe { ... }at the call site: calling a foreign function is unsafe because Rust can’t check what it does with pointers or memory. If a function really is safe to call with any arguments, you can declare it withsafe fninside theunsafe externblock (Rust 1.82+) and call it withoutunsafe. That’s a promise, not a check, andsumdoesn’t quite qualify: signed integer overflow is undefined behaviour in C, sosum(c_int::MAX, 1)is UB.c_int,c_charare aliases for whatever C’sintandcharare on the target platform.size_tmaps tousizeon all mainstream platforms.CStringowns a NUL-terminated copy of a Rust string. Rust&stris not NUL-terminated, so it can’t be passed to C directly.CString::newfails if the string contains an interior\0. Keep theCStringalive while C uses the pointer:CString::new("...").unwrap().as_ptr()in a single expression creates a dangling pointer, because the temporary is dropped at the end of the statement.- No
#[link]attribute is needed here, becauseccemits thecargo:rustc-link-libinstruction for the library it built.
Linking a prebuilt library instead
Section titled “Linking a prebuilt library instead”If the C library is already built (a system library, or a .so/.dylib/.dll you ship), skip cc and tell Cargo where it is from build.rs:
fn main() { println!("cargo:rustc-link-search=native=/path/to/lib"); println!("cargo:rustc-link-lib=dylib=sum");}With a shared library, the OS also has to find it at runtime, for example via LD_LIBRARY_PATH on Linux, DYLD_LIBRARY_PATH on macOS, or by putting the DLL next to the executable on Windows.
Next steps
Section titled “Next steps”- For a real C library with many functions and structs, generate the
externdeclarations from its header with bindgen instead of writing them by hand. - Wrap the raw
unsafefunctions in a small safe Rust API (taking&str, returningResult) so the rest of your code never touches raw pointers. - FFI chapter of the Rustonomicon
- Using
externfunctions to call external code, The Rust Programming Language
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)