Skip to content

example of writing a library that can be called from C or Rust

License

Notifications You must be signed in to change notification settings

ultrasaurus/rust-clib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-clib

An example project that uses Rust to create a library with C API.

Build rust lib and run C tests

(cd mylib && cargo build)
make test

successful output looks like this:

   Compiling add v0.2.0 (~/src/rust/clib/mylib)
    Finished dev [unoptimized + debuginfo] target(s) in 4.21s
cp mylib/target/debug/libmylib.a .
gcc  libmylib.a -o test test.o libmylib.a 
./test
PASSED
Tests run: 4

Docs I found helpful

The Rust part

in the my directory is a Rust crate, which has tests along with a very simple example that calls a function from Rust (that is also callable from C)

cd mylib
cargo test                  # test that we can call the function in Rust
cargo run --example stdin   # interactive example

references that might be helpful

Changelog

  • v0.1: exporting a Rust function that can be called from C, generating C header (blog post)
  • v0.2: export struct, functions that create/destroy, call functions that accept struct with C callback function

About

example of writing a library that can be called from C or Rust

Resources

License

Stars

Watchers

Forks

Packages