Skip to content

Calling C code from rust

  • File structure:
β”œβ”€β”€ build.rs
β”œβ”€β”€ Cargo.lock
β”œβ”€β”€ Cargo.toml
└── src
    β”œβ”€β”€ hello.c
    └── main.rs

Install cc as build-dependency

# Cargo.toml file

[build-dependencies]
cc = "1.2.38"

Create build.rs file

// build.rs file
fn main() {
    // Tell Cargo that if the given file changes, to rerun this build script.
    println!("cargo::rerun-if-changed=src/hello.c");
    // Use the `cc` crate to build a C file and statically link it.
    cc::Build::new().file("src/hello.c").compile("hello");
}

Create C file

// src/hello.c file
int add(int a, int b) {
    return a + b;
}

src/main.rs file

// Tell Rust there’s an external C function called `add`.
// Signature must match the C function exactly.
unsafe extern "C" {
    unsafe fn add(a: i32, b: i32) -> i32;
}

fn main() {
    let result = unsafe { add(2, 3) };
    println!("2 + 3 = {}", result);
}