package ctypes
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=aa797c5d6bcaee8883aece2d1d9a524fc1f63c78c1538b0fe69969c0e989419f
md5=8a301a3e3b79156448a6659859ad506c
Description
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!
To install the optional ctypes-foreign
interface (which uses libffi
to
provide dynamic access to foreign libraries), you will need to also install
the ctypes-foreign
package.
opam install ctypes-foreign
This will make the ctypes-foreign
ocamlfind subpackage available.
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 the usual 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 of sigset_t ptr
) in OCaml.
Links
Building C libraries in OCaml with the dune ctypes stanza describes how to use ctypes with dune
Chapter 19: Foreign Function Interface of Real World OCaml describes ctypes
Modular Foreign Function Bindings introduces ctypes in the context of the Mirage library operating system
Type-safe C bindings using ocaml-ctypes and stub generation introduces the Cstubs 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 an earlier paper) that presents the design of ctypes
Dependencies (5)
- bigarray-compat
- dune-configurator
- integers
-
ocaml
>= "4.03.0"
-
dune
>= "2.9"
Dev Dependencies (4)
-
odoc
with-doc
-
conf-pkg-config
with-test
-
conf-fts
with-test & os != "win32"
-
ounit2
with-test
- ahrocksdb
-
antic
< "0.4.0"
-
arakoon
>= "1.8.6" & < "1.8.12"
-
arb
< "0.4.0"
- argon2
-
arrayjit
< "0.4.1"
-
async_ssl
>= "v0.15.0"
- avroc
- bimage-unix
- bitgenerators
-
calcium
< "0.4.0"
- camlkit-base
- ceph
- cf
- checked_oint
- cmark
- cmarker
- corosync
- ctypes-build
-
ctypes-foreign
= "0.22.0"
- ctypes-zarith
- ctypes_stubs_js
- cudajit
-
decompress
>= "1.3.0" & < "1.5.3"
- directories
-
dlm
>= "0.3.3"
-
eigen
>= "0.1.4"
- eris
- extism
- flint
- fsevents
- gccjit
-
gdal
>= "0.10.0"
- gobject-introspection
- gpiod
- gr
- guile
-
hacl-star-raw
>= "0.7.2"
-
hardcaml-llvmsim
>= "0.3.0"
- hardcaml-vpi
- hardcaml_c
- hardcaml_verilator
- hdr_histogram
- imguiml
-
iocaml-kernel
>= "0.4.4"
-
kqueue
< "0.2.0"
- libbpf
- libbpf_maps
-
libdash
>= "0.3"
- libirmin
-
libudev
< "0.2.1"
- lilv
- llama-cpp-ocaml
-
llvm
>= "3.7"
- lp-glpk
- lp-gurobi
- luv
- luv_unix
-
lz4
>= "1.3.0"
-
mariadb
>= "1.1.2" & < "1.1.5"
-
memcpy
>= "0.2.2"
- mmdb
- monocypher
- mpg123
- nanomsg
- nebula
-
netlink
>= "0.3.4"
- octez-l2-libs
- octez-libs
-
opasswd
>= "1.3.1"
-
opencc
< "transition"
- opencc0
- opencc1
- opencc1_1
- orocksdb
-
osbx
< "1.1.1"
- osx-acl
-
osx-attr
>= "0.2.0"
-
osx-cf
>= "0.1.1"
-
osx-fsevents
>= "0.1.1"
- osx-membership
-
osx-mount
>= "0.1.1"
- osx-secure-transport
-
osx-xattr
>= "0.3.0"
-
owl
>= "0.9.0"
- pari
- pari-bindings
-
pci
< "1.0.1"
- pg_query
- pkcs11-driver
- pkcs11-rev
- portaudio_c_bindings
- portmidi
- posix-base
- posix-bindings
-
posix-getopt
>= "2.0.0"
- posix-signal
- posix-socket
- posix-socket-unix
- posix-time2
- posix-types
- posix-uname
-
ppx_cstubs
>= "0.7.0"
-
protocol-9p
< "0.5.1"
-
py
>= "1.1"
- quickjs
- raygui
- raylib
- reed-solomon-erasure
-
rfc1951
>= "1.3.0"
- sanlock
-
sarek
>= "20210823"
- sattools
-
sodium
>= "0.2.0" & < "0.3.0"
-
spin
>= "0.8.0"
-
spoc
>= "20170724"
- srt
- stk_iconv
- swipl
- sys-socket
- sys-socket-unix
- tensorflow
- tezos-hacl
-
tezos-hacl-glue-unix
< "11.0"
-
tezos-sapling
>= "11.1"
- tezos-wasmer
-
tgls
= "0.8.3"
- torch
-
tsdl
>= "0.8.1" & < "0.9.1" | >= "1.0.0"
-
tsdl-image
>= "0.3.0"
- tsdl-mixer
-
tsdl-ttf
>= "0.3"
-
unix-errno
>= "0.4.1" & < "0.5.0" | >= "0.6.2"
-
unix-fcntl
>= "0.3.3"
- unix-sys-resource
- unix-sys-stat
- unix-time
-
unix-type-representations
>= "0.1.1"
- uring-trace
- wasmer
- wasmtime
- xedbindings
- xxhash
- yaml
- yara
- yices2_bindings
- zstandard
-
zstd
>= "0.4"