package ctypes

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

Install

Dune Dependency

Authors

Maintainers

Sources

0.21.1.tar.gz
sha256=6f2b3b0f26c202b75ed7df3867fd9580d3e592af4944875b92ec0ee3ca7e14ff
md5=8b201d932741c5096854e5eb39139b90

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: 21 Jul 2023

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

Dependencies (5)

  1. bigarray-compat
  2. dune-configurator
  3. integers
  4. ocaml >= "4.03.0" & < "5.2"
  5. dune >= "2.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. arakoon >= "1.8.6" & < "1.8.12"
  4. arb < "0.4.0"
  5. argon2
  6. arrayjit < "0.4.1"
  7. async_ssl >= "v0.15.0"
  8. avroc
  9. bimage-unix
  10. bitgenerators
  11. calcium < "0.4.0"
  12. ceph
  13. cf
  14. checked_oint
  15. cmark
  16. cmarker
  17. ctypes-build
  18. ctypes-foreign = "0.21.1"
  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. eigen >= "0.1.4"
  26. eris
  27. extism < "1.1.0"
  28. flint
  29. fsevents
  30. gccjit
  31. gdal >= "0.10.0"
  32. gobject-introspection
  33. gpiod
  34. gr
  35. guile
  36. hacl-star-raw >= "0.7.2"
  37. hardcaml-llvmsim >= "0.3.0"
  38. hardcaml-vpi
  39. hardcaml_c
  40. hardcaml_verilator
  41. hdr_histogram
  42. imguiml
  43. iocaml-kernel >= "0.4.4"
  44. kqueue < "0.2.0"
  45. libdash >= "0.3"
  46. libirmin
  47. libudev < "0.2.1"
  48. lilv
  49. llama-cpp-ocaml
  50. llvm >= "3.7"
  51. lp-glpk
  52. lp-gurobi
  53. luv
  54. luv_unix
  55. lz4 >= "1.3.0"
  56. mariadb >= "1.1.2" & < "1.1.5"
  57. memcpy >= "0.2.2"
  58. mmdb
  59. monocypher
  60. mpg123
  61. nanomsg
  62. nebula
  63. netlink >= "0.3.4"
  64. octez-l2-libs
  65. octez-libs
  66. opasswd >= "1.3.1"
  67. opencc < "transition"
  68. opencc0
  69. opencc1
  70. opencc1_1
  71. orocksdb
  72. osbx < "1.1.1"
  73. osx-acl
  74. osx-attr >= "0.2.0"
  75. osx-cf >= "0.1.1"
  76. osx-fsevents >= "0.1.1"
  77. osx-membership
  78. osx-mount >= "0.1.1"
  79. osx-secure-transport
  80. osx-xattr >= "0.3.0"
  81. owl >= "0.9.0"
  82. pari
  83. pari-bindings
  84. pci < "1.0.1"
  85. pg_query
  86. pkcs11-driver
  87. pkcs11-rev
  88. portaudio_c_bindings
  89. portmidi
  90. posix-base
  91. posix-bindings
  92. posix-getopt >= "2.0.0"
  93. posix-signal
  94. posix-socket
  95. posix-socket-unix
  96. posix-time2
  97. posix-types
  98. posix-uname
  99. ppx_cstubs >= "0.7.0"
  100. protocol-9p < "0.5.1"
  101. py >= "1.1"
  102. quickjs
  103. raygui
  104. raylib
  105. reed-solomon-erasure
  106. rfc1951 >= "1.3.0"
  107. sanlock
  108. sarek >= "20210823"
  109. sattools
  110. sodium >= "0.2.0" & < "0.3.0"
  111. spin >= "0.8.0"
  112. spoc >= "20170724"
  113. srt
  114. stk_iconv
  115. swipl
  116. sys-socket
  117. sys-socket-unix
  118. tensorflow
  119. tezos-hacl
  120. tezos-hacl-glue-unix < "11.0"
  121. tezos-sapling >= "11.1"
  122. tezos-wasmer
  123. tgls = "0.8.3"
  124. torch
  125. tsdl >= "0.8.1" & < "0.9.1" | >= "1.0.0"
  126. tsdl-image >= "0.3.0"
  127. tsdl-mixer
  128. tsdl-ttf >= "0.3"
  129. unix-errno >= "0.4.1" & < "0.5.0" | >= "0.6.2"
  130. unix-fcntl >= "0.3.3"
  131. unix-sys-resource
  132. unix-sys-stat
  133. unix-time
  134. unix-type-representations >= "0.1.1"
  135. uring-trace
  136. wasmer
  137. wasmtime
  138. xedbindings
  139. xxhash
  140. yaml
  141. yara
  142. yices2_bindings
  143. zstandard
  144. zstd >= "0.4"

Conflicts (1)

  1. host-system-msvc
OCaml

Innovation. Community. Security.