package ctypes

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

Install

Dune Dependency

Authors

Maintainers

Sources

0.22.0.tar.gz
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.

Tags

org:mirage

Published: 19 Feb 2024

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

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
  7. async_ssl >= "v0.15.0"
  8. avroc
  9. bimage-unix
  10. bitgenerators
  11. calcium < "0.4.0"
  12. camlkit-base
  13. cf
  14. checked_oint
  15. cmark
  16. cmarker
  17. ctypes-build
  18. ctypes-foreign >= "0.22.0"
  19. ctypes-zarith
  20. ctypes_stubs_js
  21. decompress >= "1.3.0" & < "1.5.3"
  22. directories
  23. dlm >= "0.3.3"
  24. eigen >= "0.1.4"
  25. eris
  26. extism
  27. flint
  28. fsevents
  29. gccjit
  30. gdal >= "0.10.0"
  31. gobject-introspection
  32. gpiod
  33. gr
  34. guile
  35. hardcaml-llvmsim >= "0.3.0"
  36. hardcaml-vpi
  37. hardcaml_c
  38. hardcaml_verilator
  39. hdr_histogram
  40. imguiml
  41. iocaml-kernel >= "0.4.4"
  42. kqueue < "0.2.0"
  43. libbpf
  44. libbpf_maps
  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. wasmer
  136. wasmtime
  137. xedbindings
  138. xxhash
  139. yaml
  140. yara
  141. yices2_bindings
  142. zstandard
  143. zstd >= "0.4"

Conflicts

None

OCaml

Innovation. Community. Security.