/rust-c-ffi-demo

This project demonstrates how you might write a Rust program which uses its FFI to call into a shared/static C library.

Primary LanguageRustMIT LicenseMIT

About

This project demonstrates how you might write a Rust program which uses FFI to call into shared C libraries (dynamic and static). One library (libyaml) is a system-level shared library (in /usr/include on Debian 9.9) and the other is a static library (libgreet) included in this repository.

As usual, the Rust FFI docs are excellent and I was able to put this demo together without much trouble. Though, it wasn't immediately obvious how to tell cargo/rustc how to find libgreet. I eventually stumbled upon this Reddit thread and determined that I could use the LIBRARY_PATH env var when calling cargo run. (There may be other or better ways of doing this and I'll make note of them as I find them.)

This "hello world"/FFI demo is something I've started doing when learning a new language in order to 1) have an excuse to write a program and 2) learn about how the language is capable of working with its host environment and existing tools and utilities. (If interested, see my Embeddable Common Lisp and Crystal demos.)

Run

Requires:

  • gcc
  • ar
  • libyaml which exposes the yaml_get_version_string function

Build script

cargo run

Make

pushd src/lib && make && popd && LIBRARY_PATH=./src/lib cargo run