Writing static library for C in Rust

I'm trying to integrate Rust functionality with existing C codebase, and reading a lot about possibility of doing that, but can't find tutorial/example about how to do that with Cargo or Rustc compiler, or how to write C header file for that.

Sorry if this is dummy question :slight_smile: but it will be awesome to get some example for doing that.

Thanks

I'm on mobile so apologies for the short reply, but the regex crate has a C API: https://github.com/rust-lang-nursery/regex/tree/master/regex-capi

I don't know of any cohesive resources on how to put this type of thing together though.

Cool ! thanks, that's what I've been looking for.

Is there any memory leak issues that I need to worry about during linking Rust and C ? (link to docs would be great)

I have never used it myself, but your might be interested in: rusty-cheddar.
It's a tool to automatically generate C headers.

Things allocated in Rust need to be freed in Rust. Things allocated in C need to freed in C or using libc::free. Make sure your API has the right calls to make this happen. You might find Box's into_raw and from_raw functions useful.

1 Like