This project is a set of examples of FFI between Rust and C.
It uses CMake to build the C, and CMake to call Cargo to build the Rust.
This is not intended to be a Rust, C, or CMake tutorial, merely an example of
using them together. That said, comments should explain to C programmers what
the Rust code is doing.
Cargo, rustc, CMake, and a C compiler must be installed.
To install Rust (Cargo and rustc), follow the instructions at https://www.rust-lang.org/tools/install
Get CMake from https://cmake.org/download/ or your distribution's package manager (3.17+ needed)
GCC (A C compiler) can also been used, and was used to test this project. It can be downloaded from https://gcc.gnu.org/
LLVM & Clang (a C compiler) can be downloaded from https://releases.llvm.org/download.html
To cross-compile, both a C and Rust cross-compiler are needed. For Rust,
rustup target add <triple>
where is a target from the list at
https://doc.rust-lang.org/nightly/rustc/platform-support.html. For C, that's
probably a cross-compiler and toolchain from your distro's package manager.
CMake will need to define an appropriate Rust_CARGO_TARGET
value, eg
-DRust_CARGO_TARGET=armv7-unknown-linux-gnueabihf
in addition to any C/C++
target flags needed. Adding cross-compiler support is not a goal of this
project, and would require some extra scripts to call cmake
with appropriate
options.
NOTE: This has so far only been tested on Debian Bullseye. It should work for any Linux system, not sure about Mac or Windows.
The Rust Book
The Rust Reference
Reference chapter on build scripts
Rustdoc book
https://jakegoulding.com/rust-ffi-omnibus/
https://github.com/alexcrichton/rust-ffi-examples
https://doc.rust-lang.org/nomicon/ffi.html
https://github.com/eqrion/cbindgen
https://github.com/eqrion/cbindgen/blob/master/docs.md
https://rust-lang.github.io/rust-bindgen/introduction.html
https://github.com/AndrewGaspar/corrosion
https://cmake.org/cmake/help/v3.17/index.html
https://github.com/japaric/rust-cross https://rust-lang.github.io/rustup/cross-compilation.html
Rustlings provides small exercises, and is a good intro.
The Rust Book linked above as well, is comprehensive if theory-focused.
Rust-by-example is exactly what the title says.
Learning Rust with Entirely Too Many Linked Lists is all about linked lists. Linked lists aren't something you usually need to implement yourself, but they're the hardest of the data structures taught to undergraduates to implement safely.
Dancing Links in Rust provides an example of one of the rare algorithms that really needs a linked list. And two-dimensional linked list, at that!
https://ferrous-systems.com/blog/omg-wtf-rs-resources-to-help-you-get-started-with-rust/
The Rustonomicon: The Dark Arts of Unsafe Rust is a good reference to the use of Unsafe in Rust. Since C is wildly unsafe it's very common to need to make safe wrappers around unsafe C code.
The Embedded Rust Book is a guide to Rust on bare-metal (no OS, no standard library) development in Rust.