Dart + Rust FFI
Overview
This project shows how to integrate Dart with Rust using the Foreign Function Interface (FFI). The Rust code has to be compiled for the x86_64
architecture to ensure compatibility with the Globe runtime.
Bootstrap
Initialize the project using the command below
$ globe create -t ffi_hello_world
Project Structure
├── dart_project/ # Dart project source code
│ ├── bin/
│ │ └── server.dart # Entry point of the Dart application
│ ├── pubspec.yaml # Dart package configuration
├── native_hello/ # Rust source code for the library
│ ├── src/
│ │ └── lib.rs # Rust FFI implementation
│ ├── Cargo.toml # Rust package configuration
│ └── target/ # Directory for compiled Rust binaries
└── globe.yaml # Configuration for Globe
Compiling the Rust Library
The Rust library needs to be compiled for the x86_64 architecture. We recommend using the cross tool for this purpose, which simplifies cross-compilation.
- Install
cross
cargo install cross
- Compile for
x86_64-unknown-linux-gnu
Navigate to the native_hello
directory and run:
cross build --release --target x86_64-unknown-linux-gnu
The compiled library will be located in:
native_hello/target/x86_64-unknown-linux-gnu/release/libnative_hello.so
- Add the compiled library to the Dart project:
assets:
- native_hello/target/x86_64-unknown-linux-gnu/release/libnative_hello.so:static/libnative_hello.so
Deployment
globe deploy