packagectypes-foreign
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=cae47d815b27dd4c824a007f1145856044542fe2588d23a443ef4eefec360bf1
md5=b1af973ec9cf7867a63714e92df82f2a
Description
This installs thectypes-foreign
interface whichuseslibffi
to provide dynamic access to foreign libraries.
README
README.md
ctypes is a library for binding to C libraries using pure OCaml. The primary aim is to make writing C extensions as straightforward as possible.
The core of ctypes is a set of combinators for describing the structure of C types -- numeric types, arrays, pointers, structs, unions and functions. You can use these combinators to describe the types of the functions that you want to call, then bind directly to those functions -- all without writing or generating any C!
Usage
Suppose you want to bind to the following C functions:
int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signum); int sigdelset(sigset_t *set, int signum); int sigismember(const sigset_t *set, int signum);
Using ctypes you can describe the interfaces to these functions as follows:
let sigemptyset = foreign "sigemptyset" (ptr sigset_t @-> returning int) let sigfillset = foreign "sigfillset" (ptr sigset_t @-> returning int) let sigaddset = foreign "sigaddset" (ptr sigset_t @-> int @-> returning int) let sigdelset = foreign "sigdelset" (ptr sigset_t @-> int @-> returning int) let sigismember = foreign "sigismember" (ptr sigset_t @-> int @-> returning int)
The names bound by this code have the types you might expect:
val sigemptyset : sigset_t ptr -> int val sigfillset : sigset_t ptr -> int val sigaddset : sigset_t ptr -> int -> int val sigdelset : sigset_t ptr -> int -> int val sigismember : sigset_t ptr -> int -> int
That's all there is to it. Unlike theusual way of writing C extensions, there are no C "stub" functions to write, so there's much less opportunity for error.
The documentation and source distribution contain more complex examples, involving structs, unions, arrays, callback functions, and so on, and show how to create and use C values (like instances ofsigset_t ptr
) in OCaml.
Links
Building C libraries in OCaml with the dune ctypes stanza describes how to use ctypes withdune
Chapter 19: Foreign Function Interface ofReal World OCaml describes ctypes
Modular Foreign Function Bindings introduces ctypes in the context of theMirage library operating system
Type-safe C bindings using ocaml-ctypes and stub generation introduces theCstubs interface
Using Cstubs_structs shows how to use the
Cstubs_structs
module to reliably determine data layoutA modular foreign function interface is a research paper (extending anearlier paper) that presents the design of ctypes
Dependencies (6)
- conf-libffi
>= "2.0.0"
- conf-pkg-config
- dune-configurator
- ctypes
= version
- ocaml
>= "4.03.0"
- dune
>= "2.9"
Dev Dependencies (7)
- odoc
with-doc
- conf-fts
with-test & os != "win32"
- stdlib-shims
with-test
- conf-ncurses
with-test
- ounit2
with-test
- lwt
with-test & >= "2.4.7"
- integers
with-test & >= "0.2.2"
Used by (84)
- argon2
- arrayjit
- async_ssl
- bimage-unix
- bls12-381
= "0.3.15"
- bls12-381-legacy
< "0.4.4"
- bls12-381-unix
< "1.0.2"
- camlkit-base
- cf
- checked_oint
< "0.4.1"
- cmark
- cmarker
- corosync
- ctypes
< "0.21.1"
- ctypes-zarith
- cudajit
- directories
< "0.3"
- extism
- flock
- gccjit
- gdal
- gobject-introspection
- gpiod
- gr
- guile
- hacl-star-raw
< "0.4.4"
- hardcaml_c
- hardcaml_verilator
- hdr_histogram
- imguiml
- libbpf_maps
- libdash
- libirmin
- libudev
- lilv
- llama-cpp-ocaml
- lmdb
< "1.0"
- lp-glpk
< "0.4.0"
- lp-gurobi
- memtrace_viewer
< "v0.15.0"
- mmdb
- mpg123
- netlink
- ocephes
- octez-l2-libs
- opasswd
- opencc
< "transition"
- opencc0
- opencc1
- opencc1_1
- orocksdb
- osx-cf
- osx-secure-transport
- pari
- pari-bindings
- pg_query
- pkcs11-driver
- pkcs11-rev
- portaudio_c_bindings
- portmidi
- posix-getopt
< "2.0.0"
- py
- qcstm
>= "0.1.1"
- reed-solomon-erasure
- sarek
- sattools
- sodium
< "0.6.0"
- spoc
- srt
>= "0.2.2"
- stk_iconv
- swipl
- tensorflow
- tezos-wasmer
- tgls
- torch
- tsdl
- tsdl-image
- tsdl-mixer
- tsdl-ttf
- unix-type-representations
< "0.1.1"
- wasmer
- wasmtime
- yara
- yices2_bindings
Conflicts
None