The libc crate exposes C-compatible types, constants, and function declarations for each platform Rust targets. Items live under the libc root and match the platform being compiled.
Overview
libc supplies raw foreign-function interface bindings so Rust code can call into platform C libraries and POSIX-style APIs. It includes type aliases, errno values, structs, and extern function symbols such as malloc, aligned with the OS and libc used at compile time.
Everything is exported from the crate root as libc::item names. Windows WinAPI bindings are intentionally omitted; separate crates address Windows APIs. Design rationale is documented in the project's RFC.
Consumers add libc as a Cargo dependency. Platform-specific API surface is documented on docs.rs per target triple, and continuous integration lists supported build targets and test matrices.
Documentation on docs.rs emphasizes safe usage patterns with MaybeUninit, avoiding assumptions about constant sizes, and handling padding or reserved fields that change across libc versions. Cargo features cover std linkage behavior and optional trait derives, with some legacy features slated for removal in a future major release.
Key features
- Per-platform C API bindings
- Types, constants, and extern fn
- docs.rs per-target docs
- Cargo feature flags
- Dual Apache-2.0 and MIT license
- Tiered platform support policy
- Usage guidelines in docs
- No Windows API in crate
Use cases
- Calling OS APIs from Rust
- Low-level systems programming
- Interop with existing C libraries
- Portable syscall wrappers
- no_std platform integration