Skip to content

OCaml bindings for Zig FFI - call Zig libraries from OCaml

License

Notifications You must be signed in to change notification settings

hyperpolymath/ocaml-zig-ffi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OCaml-Zig-FFI

OCaml bindings for calling Zig libraries via FFI.

Overview

This library provides type-safe OCaml bindings to Zig libraries. Zig exports C-compatible functions that OCaml can call through its C FFI mechanism using ctypes or external declarations.

Architecture

OCaml → Ctypes → Zig (C ABI)

Using the ctypes library for high-level, type-safe FFI.

Installation

opam install ocaml-zig-ffi

Or add to your dune-project:

(depends
  (ocaml-zig-ffi (>= 0.1)))

Usage

Using Ctypes

open Zig_ffi

(* Load the library *)
let lib = Zig_ffi.load "./libmyzig.so"

(* Call functions *)
let result = Zig_ffi.add lib 2 3
(* result = 5 *)

Low-Level External Declarations

external add : int -> int -> int = "zig_add"
external multiply : int -> int -> int = "zig_multiply"

Building Zig Libraries for OCaml

Your Zig code must export C-compatible functions:

// mylib.zig
export fn add(a: i32, b: i32) callconv(.C) i32 {
    return a + b;
}

Build as shared library:

zig build-lib -dynamic -O ReleaseFast mylib.zig

Type Mappings

OCaml Type Zig Type Ctypes Type

int

i32 / i64

int

int32

i32

int32_t

int64

i64

int64_t

float

f64

double

string

[*:0]const u8

string

bytes

[*]u8

ocaml_bytes

Examples

See the examples/ directory for complete working examples.

RSR Compliance

This library follows the Rhodium Standard Repository guidelines:

  • OCaml for AffineScript compiler

  • Zig for performance-critical FFI

  • No C wrapper code required

License

PMPL-1.0-or-later

About

OCaml bindings for Zig FFI - call Zig libraries from OCaml

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •