Skip to content

tweedegolf/crc-in-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prerequisites

  • A C compiler

Steps

  1. cargo new crc-in-c and cd crc-in-c

  2. Copy crc.c and crc.h

  3. Add the cc build dependency, by adding to Crate.toml the lines:

    [build-dependencies]
    cc = "1.0"
  4. Create build.rs with contents

    extern 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

  5. Define an extern (fill in the argument and return types)

    extern "C" {
        fn CRC32( ... ) -> ...; // hint: https://doc.rust-lang.org/std/os/raw
    }
  6. Create a rust wrapper that calls the extern function

    fn crc32( ... ) -> ... { 
        ... // (hints: `unsafe`, `.as_ptr()`, `.len()`)
    }
  7. Call our wrapper on some example input

    fn main() {
        println!("{}", crc32(b"12345678"));
    }

About

Use crc32 written in C from a Rust application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published