package ctypes

  1. Overview
  2. Docs
Combinators for binding to C libraries without writing any C

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.24.0.tar.gz
sha256=249c5604c8554659761a7282db4ad200aa8c0fdc408cdb53102bd70feeb51955
md5=064316aaf508a9db203f114bb8401dee

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.

Tags

org:mirage

Published: 13 Nov 2025

README

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!

GitHub Actions status

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.

Dependencies (4)

  1. dune-configurator
  2. integers
  3. ocaml >= "4.07.0"
  4. dune >= "3.9"

Dev Dependencies (4)

  1. odoc with-doc
  2. conf-pkg-config with-test
  3. conf-fts with-test & os != "win32"
  4. ounit2 with-test

  1. ahrocksdb
  2. antic < "0.4.0"
  3. arb < "0.4.0"
  4. argon2
  5. arrayjit
  6. async_ssl >= "v0.15.0"
  7. bimage-unix
  8. bitgenerators
  9. calcium < "0.4.0"
  10. camlkit-base
  11. ceph
  12. cf
  13. checked_oint < "0.4.1"
  14. cmark
  15. cmarker
  16. corosync
  17. ctypes-build
  18. ctypes-foreign >= "0.24.0"
  19. ctypes-zarith
  20. ctypes_stubs_js
  21. cudajit
  22. decompress >= "1.3.0" & < "1.5.3"
  23. directories
  24. dlm >= "0.3.3"
  25. dune >= "3.17.2"
  26. echo
  27. eigen >= "0.1.4"
  28. extism
  29. flint
  30. fsevents
  31. gccjit
  32. gdal >= "0.10.0"
  33. gl-legacy
  34. gobject-introspection
  35. gpiod
  36. gr
  37. guile
  38. hacl-star-raw >= "0.7.2"
  39. hardcaml_c
  40. hardcaml_verilator
  41. hdr_histogram
  42. imguiml
  43. kqueue < "0.2.0"
  44. libbpf
  45. libbpf_maps
  46. libdash >= "0.3"
  47. libdrm
  48. libirmin
  49. lilv
  50. llama-cpp-ocaml
  51. llvm >= "3.7"
  52. lp-glpk
  53. lp-gurobi
  54. luv
  55. luv_unix
  56. lz4 >= "1.3.0"
  57. mariadb < "1.1.5" | >= "1.2.0"
  58. memcpy >= "0.2.2"
  59. metal
  60. mmdb
  61. mpg123
  62. netlink >= "0.3.4"
  63. nx < "1.0.0~alpha1"
  64. octez-l2-libs
  65. octez-libs
  66. opasswd
  67. opencc < "transition"
  68. opencc0
  69. opencc1
  70. opencc1_1
  71. orocksdb
  72. osx-acl
  73. osx-attr
  74. osx-cf
  75. osx-fsevents
  76. osx-membership
  77. osx-mount
  78. osx-secure-transport
  79. osx-xattr
  80. owl >= "0.9.0"
  81. pari
  82. pari-bindings
  83. pg_query
  84. pkcs11-driver
  85. pkcs11-rev
  86. portaudio_c_bindings
  87. portmidi
  88. posix-base
  89. posix-bindings < "3"
  90. posix-errno
  91. posix-getopt >= "2.0.0"
  92. posix-math2
  93. posix-resource
  94. posix-signal
  95. posix-socket
  96. posix-socket-unix
  97. posix-stat
  98. posix-time2
  99. posix-types
  100. posix-uname
  101. posix-unistd
  102. ppx_cstubs >= "0.7.0"
  103. py >= "1.1"
  104. quickjs
  105. raygui < "1.5.1"
  106. raylib
  107. reed-solomon-erasure
  108. rfc1951 >= "1.3.0"
  109. rune
  110. sanlock
  111. sarek
  112. sattools
  113. sodium < "0.6.0"
  114. spin >= "0.8.0"
  115. spoc
  116. srt
  117. stk_iconv
  118. swipl
  119. sys-socket
  120. sys-socket-unix
  121. tensorflow
  122. tezos-hacl
  123. tezos-hacl-glue-unix < "12.3"
  124. tezos-sapling
  125. tezos-wasmer
  126. tgls >= "0.9.0"
  127. tinyfiledialogs
  128. torch
  129. tsdl >= "1.1.0"
  130. tsdl-image >= "0.3.0"
  131. tsdl-mixer
  132. tsdl-ttf >= "0.3"
  133. unix-errno >= "0.4.1" & < "0.5.0" | >= "0.6.2"
  134. unix-sys-resource
  135. unix-sys-stat
  136. unix-time
  137. unix-type-representations >= "0.1.1"
  138. uring-trace
  139. wasmer
  140. wasmtime
  141. xedbindings
  142. xxhash
  143. yaml
  144. yara
  145. yices2_bindings
  146. zstandard
  147. zstd >= "0.4"

Conflicts (1)

  1. host-system-msvc