CXX: Safe interoperability between Rust and C++
Safe FFI library for calling C++ from Rust and vice versa with zero-overhead bindings via code generation.
Learn more about CXX
CXX is a foreign function interface (FFI) library that facilitates safe interoperability between Rust and C++ codebases. It works by defining FFI boundaries within Rust modules using a bridge macro, then performing static analysis on both sides to ensure type safety and ABI compatibility. The library uses dual code generators - a Rust procedural macro and a C++ code generator - to emit the necessary extern "C" signatures and static assertions. CXX supports native types from both languages including Rust's String/Vec/Box and C++'s std::string/std::vector/std::unique_ptr, making it suitable for integrating existing C++ libraries into Rust applications or vice versa.
Static Safety Analysis
Performs compile-time verification of FFI boundaries to uphold both Rust's and C++'s type system invariants. Uses static assertions to ensure signature accuracy between languages.
Zero-Overhead Bindings
Generates ABI-compatible signatures without runtime checks, memory allocation, copying, or serialization. Operates at native performance levels.
Native Type Support
Provides built-in bindings for standard library types from both languages with idiomatic APIs. Supports complex types like smart pointers and containers across the FFI boundary.
#[cxx::bridge]
mod ffi {
// Shared struct visible to both Rust and C++
struct BlobMetadata {
size: usize,
tags: Vec<String>,
}
extern "Rust" {
// Opaque Rust type
type MultiBuf;
// Rust function callable from C++
fn next_chunk(buf: &mut MultiBuf) -> &[u8];
}
unsafe extern "C++" {
include!("blobstore.h");
// Opaque C++ type
type BlobstoreClient;
// C++ functions callable from Rust
fn new_blobstore_client() -> UniquePtr<BlobstoreClient>;
fn put(&self, parts: &mut MultiBuf) -> u64;
fn metadata(&self, blobid: u64) -> BlobMetadata;
}
}Drop Bazel 7 support.
- –Drop Bazel 7
Support for Bazel 9.0.0.
- –Support for Bazel 9.0.0
Add support for Vec<Box<T>>.
- –Add support for `Vec<Box<T>>`
Top in Developer Tools
Related Repositories
Discover similar tools and frameworks used by developers
Keiyoushi Extensions
Extension management for the Keiyoushi multi-source content aggregation platform.
bolt.new
LLM-powered browser IDE with integrated WebContainers runtime.
Lima
Linux VM launcher with automatic file sharing, port forwarding, and container engine support.
JSON for Modern C++
Header-only C++ library for JSON with STL-like containers.
jsdom
Pure JavaScript implementation of web standards for Node.js.