Skip to content

This repository is a small example of how to implement native C libraries inside your Dart or Flutter projects, using the FFI and FFIGen packages.

Notifications You must be signed in to change notification settings

danielalvarezm/ffi-how-to-use

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FFI/FFIGEN HOW TO USE EXAMPLE

This repository is an example of how to integrate a C project into your Dart/Flutter project using ffi and ffigen Dart packages.

In this case, a C project has been created with a library containing a function that multiplies two numbers. This library is used in a Dart project (it would also work for Flutter) to display the result of the multiplication in the console.

Index

Getting started

  • Install taskfile to automate processes. If you don't want to use it, you can run the commands in the Taskfile.yaml file manually.
  • Install ffigen dependencies.
  • Install project dependencies with dart pub get.
  • You should have everything you need to compile and run a C project. If not, install the necessary dependencies (Cmake, gcc, etc).
  • Run task generate to generate all dependencies and bindings needed. This includes:
    • dart pub get: Install dart dependencies
    • task build: Generate C shared library, dependencies and executable
    • task ffigen: Generate dart code with ffigen from C lang
  • Run task execute or task run (generate + execute) to run the example.

💡 All task commands have to be run from the root folder of the project.

Project structure

├── /bin # Dart code interacting with native code
├── /lib # Bindings generated for native code
├── /native_code # C code
│  ├── /build # Build files, executables, shared library, etc (this folder is generated automatically if you use taskfile).
│  ├── /multiply # Example of a library
│  ├── main.c # Example of library use
│  ├── CMakeLists.txt # To build the native code
├── pubspec.yaml # Dart dependencies
├── taskfile.yml # File with different tasks to automate processes
...

About

This repository is a small example of how to implement native C libraries inside your Dart or Flutter projects, using the FFI and FFIGen packages.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published