- A C compiler
-
cargo new crc-in-c
andcd crc-in-c
-
Copy
crc.c
andcrc.h
-
Add the
cc
build dependency, by adding toCrate.toml
the lines:[build-dependencies] cc = "1.0"
-
Create
build.rs
with contentsextern crate cc; fn main() { cc::Build::new().file("crc32.c").compile("crc32.a"); }
This will find your c code, compile it, and link it into the executable rust produces
-
Define an extern (fill in the argument and return types)
extern "C" { fn CRC32( ... ) -> ...; // hint: https://doc.rust-lang.org/std/os/raw }
-
Create a rust wrapper that calls the extern function
fn crc32( ... ) -> ... { ... // (hints: `unsafe`, `.as_ptr()`, `.len()`) }
-
Call our wrapper on some example input
fn main() { println!("{}", crc32(b"12345678")); }