Ada bindings for calling Zig libraries via FFI.
This library provides type-safe Ada bindings to Zig libraries.
Zig exports C-compatible functions that Ada can call through
its Interfaces.C mechanism.
Ada → Interfaces.C → Zig (C ABI)
Ada’s pragma Import(C, …) can directly call C-compatible
functions exported by Zig shared libraries.
with Interfaces.C;
use Interfaces.C;
package Zig_Lib is
-- Import Zig function
function Add (A, B : int) return int
with Import => True,
Convention => C,
External_Name => "add";
function Multiply (A, B : int) return int
with Import => True,
Convention => C,
External_Name => "multiply";
end Zig_Lib;| Ada Type | Zig Type | Notes |
|---|---|---|
|
|
C int |
|
|
C long |
|
|
C unsigned |
|
|
64-bit float |
|
|
String |
|
|
Raw pointer |
Ada’s strong typing provides additional safety when calling Zig code:
-
Type mismatches are caught at compile time
-
Range checks can validate Zig return values
-
Pre/postconditions can wrap FFI calls
The examples/ directory contains complete working examples:
Demonstrates calling Zig functions from Ada:
cd examples
gprbuild -P examples.gpr basic_example.adb
./bin/basic_exampleFeatures demonstrated: * Version information retrieval * Arithmetic operations (add, multiply) * Mathematical functions (factorial, fibonacci) * String operations
Demonstrates bidirectional FFI with callbacks from Zig to Ada:
cd examples
gprbuild -P examples.gpr callback_example.adb
./bin/callback_exampleFeatures demonstrated: * Registering Ada procedures as callbacks * Invoking callbacks from Zig * Iterator pattern with Ada-style termination control
This library follows the Rhodium Standard Repository guidelines:
-
Ada for safety-critical systems
-
Zig for performance-critical FFI
-
No C wrapper code required