An open API service indexing awesome lists of open source software.

https://github.com/anzhi0708/example-rust-c-ffi

Minimal example for calling C function from Rust code
https://github.com/anzhi0708/example-rust-c-ffi

build build-system c cargo clang example examples examples-rust ffi foreign-function-interface rust std stdlib

Last synced: 8 days ago
JSON representation

Minimal example for calling C function from Rust code

Awesome Lists containing this project

README

          

# Minimal Rust C FFI example

## Keypoints
- Add `build = "src/build.rs"` in `Cargo.toml` (otherwise cargo sometimes fails to pick it up and I don't know why)
- `#[link(name = "hello", kind = "dylib")]` (or another kind; in this case a dylib)
- `cargo:rustc-link-search` flag in `build.rs` file
- Set environment variables correctly (Linux: `LD_LIBRARY_PATH`, Mac: `DYLD_LIBRARY_PATH`)

`build_and_run` bash script sets enviroment variables for you; you may want to take a look at it.