package ctypes

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

Install

Dune Dependency

Authors

Maintainers

Sources

0.23.0.tar.gz
sha256=cae47d815b27dd4c824a007f1145856044542fe2588d23a443ef4eefec360bf1
md5=b1af973ec9cf7867a63714e92df82f2a

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

Conflicts (1)

  1. host-system-msvc
OCaml

Innovation. Community. Security.