How to call rust library in c


How i call rust functions in c language and vice versa . will you explain with simple example. PLEASE.


To call Rust from C:

extern "C" fn foo() {
    // stuff

To call C from Rust:

extern "C" {
    fn foo();

fn bar() {


There’s an explanation with examples in this blog post:


Take a look at the ffi-omnibus. It has examples for both C and other languages


You can’t just use pub extern "C" to call a function from Rust :frowning:

Visibility and exporting of functions in inconsistent in Rust. It is done differently depending on whether Rust or C will call them.

For C both visibility and exporting are done with the #[no_mangle] attribute instead of pub and accessibility from the crate root.