hhstore / blog

My Tech Blog: about Mojo / Rust / Golang / Python / Kotlin / Flutter / VueJS / Blockchain etc.
https://github.com/hhstore/blog/issues
276 stars 22 forks source link

Flutter/Dart + FFI + Zig: call a Zig function from Dart using FFI #384

Open hhstore opened 1 year ago

hhstore commented 1 year ago

related:

flutter 插件:

zig 篇:

一些讨论:

ref:

hhstore commented 1 year ago

Flutter 使用 FFI 调用 Zig 示例:

之前 c 调用 zig 的示例:

应用场景:

基本步骤:

✅ zig 侧: 写 zig 代码.

  1. 写 zig 代码: https://github.com/better-dart/learn-dart/blob/main/packages/ffi-core-zig/src/main.zig#L4
  2. 基于 zig v0.6 生成 c 头文件
  3. 运行 zig build, 生成 二进制 bin 文件: ffi-core-zig/zig-out/lib 路径下.

✅ dart 侧:

  1. 基于 dart ffigen 工具, 生成 binding 代码 generated_bindings.
  2. 运行 example/main.dart
    • 指定 上面生成的 zig bin 库文件.
  3. 运行结果日志:

➤ task binding:run
task: [binding:run] fvm dart run ./example/main.dart
baseDir: Directory: '/Users/dev/Documents/iSpace/better-dart/learn-dart/packages'
zigBinDir: /Users/dev/Documents/iSpace/better-dart/learn-dart/packages/ffi-core-zig/zig-out/lib
call zig add, 2

✅ 关键代码:

image

详细的运行脚本:

✅ zig gen ffi and run:

✅ dart run example:

hhstore commented 1 year ago

zig 交叉编译截图:


  # https://ziglang.org/download/0.8.0/release-notes.html#Support-Table
  build:
    cmds:
      - zig build
      - zig build -Dtarget=x86_64-linux
      - zig build -Dtarget=x86_64-macos
      - zig build -Dtarget=x86_64-windows
      - zig build -Dtarget=aarch64-macos
      - ls -al  zig-out/lib/
      # - ls -al .
    ignore_error: true
image
hhstore commented 1 year ago

Zig FFI 扩展阅读:

zig @cImport():

关于 zig CC / zig C++:

image

hhstore commented 1 year ago

1

hhstore commented 1 year ago

1

hhstore commented 1 year ago

1

dhrubrawat66 commented 3 months ago

this is not working please maintain it...