brickpop / flutter-rust-ffi Goto Github PK
View Code? Open in Web Editor NEWStarter project for Flutter plugins willing to access native and synchronous rust code using FFI
License: Other
Starter project for Flutter plugins willing to access native and synchronous rust code using FFI
License: Other
when i use make all
= note: ld: error: unable to find library -lgcc
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)
error: aborting due to previous error
error: could not compile example-rs
To learn more, run the command again with --verbose.
make: *** [target/aarch64-linux-android/release/libexample.so] Error 101
Follow https://flutter.dev/docs/development/platform-integration/c-interop#ios-symbols-stripped
See from https://github.com/drewcrawford/Rust-iOS-Example#the-case-of-the-missing-symbols
What seems to happen is Xcode strips some symbols from a framework when it's built. So if you have libhello.a inside a framework, and the framework doesn't call anything inside libhello.a, all of libhello.a is striped. If you only call one function, the others are stripped etc.
So what you have to do is declare an exported_symbols.txt
like
_hello
_benchmark
And then set that file as the framework's Exported Symbols File
in the framework's build settings.
From the README:
NOTE: By default, XCode will skip bundling the libexample.a library if it detects that it is not being used. To force its inclusion, add a dummy method in SwiftMylibPlugin.swift that uses at least one of the native functions.
Do you know how to force Xcode to always bundle static libs? In my case, I'm not using a Flutter plugin, and just trying to use a single universal .a file.
Hi!
Copied this example. My pubspec:
dependencies:
flutter:
sdk: flutter
ffi: ^1.1.2
dev_dependencies:
flutter_test:
sdk: flutter
ffigen: 4.1.3
ffigen:
name: NativeLibrary
description: Bindings to `headers/example.h`.
output: 'lib/generated_bindings.dart'
headers:
entry-points:
- 'rust/bindings.h'
My header is only 1 row and is: void start_application(void);
.
So when I run flutter pub run ffigen
it creates a dart file with errors like: Variables must be declared using the keywords 'const', 'final', 'var' or a type name. Try adding the name of the type of the variable or the keyword 'var'.
and then The instance member '_start_applicationPtr' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression
Any clue why I get these errors?
Is it possible to call flutter functions from Rust? I know it's possible when using JNI-rust (by interacting with JniEnv
methods), but I'm not sure if it's possible in the dart/flutter context
Is it possible to call Rust from Flutter Web using wasm?
It was ok in debug mode, but failed in release mode.
Failed to lookup symbol (dlsym(RTLD_DEFAULT, test_func): symbol not found)
#0 DynamicLibrary.lookup (dart:ffi-patch/ffi_dynamic_library_patch.dart:33)
Hi thanks for this template! I wonder whether Rust+Flutter is ok for real-world production app? Or it is only suitable for toy projects?
Hi thanks for this wonderful starter! As shown in the sample code of this repo, we have to manually write down allocations/frees for pointers such as Strings. That is still quite error-prone and cumbersome. So maybe we can use flutter_rust_bridge
, which will automatically generate all such bindings.
Hello and thanks for the template and article.
I'm trying to use the produced .a
file as a static library, I'm including the .h
on the bridge header file and everything works on debug on a real-device and emulator.
However when I archive and test from Testflight I get a grey screen, if I comment out all the Rust invocations from Flutter, the App works as expected.
Is there something specific that needs to be done in order for the library to be included? I have it on embedded libraries and frameworks. Not sure what else to do.
Thanks in advance!
Thank you for solving this ! I've been working hard on this too and I don't think I would have found the trick of adding a dummy function to my swift library. That's a weird behavior, don't you think there is something we can do ? Maybe the Dart team can have a look at this.
For the android part I prefer to use cargo-gradle because it rebuilds when my Rust code changes you should definitely check it out.
The Dart bindings process could be automated by importing ffigen and making it parse the C header file
make script not working on Windows.
As the title?
I plan to use rust to take some safety measures to prevent "cracking".
Thank you for the template, it is really useful!
Rust has few libraries that allows database access like sqlite and also native storat. Is it possible to do it with rust and access rust function via flutter?
Basically I want push as much as on Rust side as I'm not that happy with flexibility of dart language.
BTW thanks for awesome template. :-)
Why do you left x86_64,
until now flutter release support arm64, arm arches but in latest they added x86_64 for latest target, so it is better to add x86_64 target also.
I tried to write some test, under both ./test
and ./example/test
but it comes with error:
Invalid argument(s): Failed to lookup symbol (dlsym(RTLD_DEFAULT, my_function_name): symbol not found)
dart:ffi DynamicLibrary.lookup
Though it can be called in the application.
I tried to add some delay for loading the test, but still failed, probabaly some extra setting is required.
It should more related to ffi
package, discussed it there: dart-archive/ffi#39
This guy made a very very very good Dart<->Flutter FFI automation/generation, but the project is Sponsorware:
https://github.com/thlorenz/rid-examples
I just wanted to make it more visible to more people can sponsor it and thus make it open source. Maybe people from this repo can give him a sponsorship :)
flutter run lib/mylib.dart
$ flutter run lib/mylib.dart
Using hardware rendering with device Android SDK built for x86. If you notice graphics artifacts, consider enabling software rendering with
"--enable-software-rendering".
Launching lib/mylib.dart on Android SDK built for x86 in debug mode...
Exception: [!] Your app is using an unsupported Gradle project. To fix this problem, create a new project by running flutter create -t app <app-directory>
and then move the dart code, assets and pubspec.yaml to the new project.
The same happens after renaming mylib.dart
to main.dart
and do flutter run
.
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.1, on Mac OS X 10.15.4 19E287 darwin-x64, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.55.2)
[✓] Connected device (2 available)
• No issues found!
Hi,
I am asking myself if I can use this template to call native rust functions and then write to the standard input of the rust
library as well as read from its standard output. Do you know if this is possible? I am asking because I am trying
to integrate a UCI Chess Engine (Stockfish, more specifically its Rust port called Rustfish) into a Flutter App and therefore I need to be able to write to/ read from the standard input/ output to send UCI commands and receive their answers.
I would be very happy if you could help me here.
Is there some way to generate linux bindings, I'm building for linux arm64 and I need to access my rust code. I'm building a dbus wrapper if I can't but I'd like to access it directly.
First of all, thank you for this nice template. I am currently trying to integrate the Stockfish chess engine into Flutter using a Rust port of Stockfish (https://github.com/syzygy1/Rustfish) together with your template. However, I am not able to get it running at the moment and I don't know what the problem is.
The lib.rs file I use for this template is the Rustfish source code bundled into a single file (adjusting the function signature of the main function I wand to call as stated in the readme of your template). The bundled rust library works fine as it is but after integrating it into flutter using the template, it will only crash when calling the main function of the library. All I get is the following error stacktrace where I sadly can't make anything out of:
I/flutter (14269): - Mylib bindings found 👍
I/flutter (14269): DynamicLibrary: handle=0xbea78c8197f77fe5
I/flutter (14269): - Calling native rustfish test
I/flutter (14269): - Executed
I/flutter (14269): - Calling native rustfish main
F/libc (14269): Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 14318 (1.ui), pid 14269 (b.mylib_example)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: ****
Revision: '0'
ABI: 'arm64'
Happend: 'Sat Nov 28 18:46:14 2020
'
SYSVMTYPE: Art
APPVMTYPE: Art
pid: 14269, tid: 14318, name: 1.ui >>> org.mylib.mylib_example <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
x0 0000000000000000 x1 00000000000037ee x2 0000000000000006 x3 0000000000000008
x4 00000070e6f5f1c8 x5 00000070e6f5f1c8 x6 00000070e6f5f1c8 x7 00000070e6f5f000
x8 0000000000000083 x9 539e262590de5120 x10 0000000000000000 x11 fffffffc7ffffbdf
x12 0000000000000001 x13 0000000000002710 x14 0000000051eb851f x15 0000000000000064
x16 00000071942f12c0 x17 000000719422fe34 x18 0000000000000001 x19 00000000000037bd
x20 00000000000037ee x21 00000070e7019dc0 x22 00000070e70eda70 x23 00000070e6ff8208
x24 0000000000000001 x25 0000000000000000 x26 0000000000011803 x27 0000000000015ac4
x28 fffffffffffffffe x29 00000070f3bfdc30
sp 00000070f3bfdbf0 lr 0000007194224960 pc 0000007194224988
backtrace:
#00 pc 0000000000022988 /system/lib64/libc.so (abort+116)
#01 pc 000000000006a72c /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#02 pc 0000000000067dc4 /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#03 pc 0000000000068d3c /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#04 pc 0000000000068cc4 /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#05 pc 0000000000068840 /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#06 pc 0000000000066fec /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#07 pc 0000000000068808 /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#08 pc 000000000007b988 /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#09 pc 000000000007b95c /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#10 pc 00000000000496b0 /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#11 pc 00000000000492f0 /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so
#12 pc 0000000000030784 /data/app/org.mylib.mylib_example-u95oBwvD6alTIL-bUUZg3g==/lib/arm64/librustfish.so (main+188)
#13 pc 0000000000005564 <anonymous:00000070f2a00000>
The dart file where I try to call the native rust code is the following:
import 'dart:ffi';
import 'dart:io';
///////////////////////////////////////////////////////////////////////////////
// Typedef's
///////////////////////////////////////////////////////////////////////////////
typedef RustMainFunc = void Function();
typedef RustMainFuncNative = Void Function();
typedef RustTestFunc = void Function();
typedef RustTestFuncNative = Void Function();
///////////////////////////////////////////////////////////////////////////////
// Load the library
///////////////////////////////////////////////////////////////////////////////
final DynamicLibrary nativeRustfishLib = Platform.isAndroid
? DynamicLibrary.open("librustfish.so")
: DynamicLibrary.process();
///////////////////////////////////////////////////////////////////////////////
// Locate the symbols we want to use
///////////////////////////////////////////////////////////////////////////////
final RustMainFunc rustMain = nativeRustfishLib
.lookup<NativeFunction<RustMainFuncNative>>("main")
.asFunction();
final RustTestFunc rustTest = nativeRustfishLib
.lookup<NativeFunction<RustTestFuncNative>>("test")
.asFunction();
///////////////////////////////////////////////////////////////////////////////
// HANDLERS
///////////////////////////////////////////////////////////////////////////////
String nativeRustfishMain() {
if (nativeRustfishLib == null)
return "ERROR: The native rustfish library is not initialized 🙁";
print("- Mylib bindings found 👍");
print(" ${nativeRustfishLib.toString()}"); // Instance info
print("- Calling native rustfish test");
// The actual native call
rustTest(); // working dummy method call
print("- Executed");
print("- Calling native rustfish main");
// The actual native call
rustMain(); // crashing
print("- Executed");
return "Success";
}
I would be very glad about any king of help on setting this up.
I have been trying to setup a project to use Flutter and Golang together for macOS.
I followed two of your articles https://medium.com/stack-me-up/meet-the-coolest-tech-marriage-flutter-go-369cf11367c9 and https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2 but unfortunately I could not get the setup happen.
I got lost at macOS bundle security and dylib linking using xcode.
How do I expose the golang native functions into flutter app for desktop?
Could you write a similar article on how to use Golang and Flutter together for desktop or any pointers will be appreciated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.