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.
A detailed guide can be found here
- 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 dependenciestask build
: Generate C shared library, dependencies and executabletask ffigen
: Generate dart code with ffigen from C lang
- Run
task execute
ortask run
(generate + execute) to run the example.
💡 All task commands have to be run from the root folder of the project.
├── /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
...