Welcome to the Dart FFI project! This repository contains code and examples demonstrating how to use Dart FFI (Foreign Function Interface) to call C code from Dart.
In this project, you'll learn how to:
- Set up your environment for using Dart FFI.
- Write and compile C code.
- Integrate and call C functions from your Dart application.
Before you begin, ensure you have the following installed on your machine:
- Dart SDK
- C compiler (such as GCC for Linux/macOS or MinGW for Windows)
-
Clone the repository:
git clone https://github.com/khkred/ffi_factorial.git cd ffi_factorial
-
Compile the C code:
Compile the C code into a shared library. The commands may vary depending on your operating system:
-
Linux/macOS:
gcc -shared -o libfactorial.so factorial.c
-
Windows:
gcc -shared -o libfactorial.dll factorial.c
-
-
Run the Dart application:
dart run
The Dart application will prompt you to enter a number to calculate its factorial using the native C function. Simply follow the on-screen instructions.
- factorial.c: Contains the C code for calculating the factorial.
- libfactorial.so/libfactorial.dll/libfactorial.dylib: The compiled shared library.
- ffi_factorial.dart: The Dart code that uses FFI to call the C function.
- factorial_user_input.dart: The Dart code that asks for an user input in order to calcualte the factorial
Contributions are welcome! If you have any suggestions or improvements, please feel free to submit a pull request or open an issue.
This project is licensed under the MIT License. See the LICENSE file for more details.
Special thanks to the Dart and Flutter communities for their continuous support and inspiration.