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. eigen >= "0.1.4"
  28. eris
  29. extism
  30. flint
  31. fsevents
  32. gccjit
  33. gdal >= "0.10.0"
  34. gobject-introspection
  35. gpiod
  36. gr
  37. guile
  38. hardcaml-llvmsim >= "0.3.0"
  39. hardcaml-vpi
  40. hardcaml_c
  41. hardcaml_verilator
  42. hdr_histogram
  43. imguiml
  44. iocaml-kernel >= "0.4.4"
  45. kqueue < "0.2.0"
  46. libbpf
  47. libbpf_maps
  48. libdash >= "0.3"
  49. libirmin
  50. libudev < "0.2.1"
  51. lilv
  52. llama-cpp-ocaml
  53. llvm >= "3.7"
  54. lp-glpk
  55. lp-gurobi
  56. luv
  57. luv_unix
  58. lz4 >= "1.3.0"
  59. mariadb >= "1.1.2" & < "1.1.5"
  60. memcpy >= "0.2.2"
  61. mmdb
  62. monocypher
  63. mpg123
  64. nanomsg
  65. nebula
  66. netlink >= "0.3.4"
  67. octez-l2-libs
  68. octez-libs
  69. opasswd >= "1.3.1"
  70. opencc < "transition"
  71. opencc0
  72. opencc1
  73. opencc1_1
  74. orocksdb
  75. osbx < "1.1.1"
  76. osx-acl
  77. osx-attr >= "0.2.0"
  78. osx-cf >= "0.1.1"
  79. osx-fsevents >= "0.1.1"
  80. osx-membership
  81. osx-mount >= "0.1.1"
  82. osx-secure-transport
  83. osx-xattr >= "0.3.0"
  84. owl >= "0.9.0"
  85. pari
  86. pari-bindings
  87. pci < "1.0.1"
  88. pg_query
  89. pkcs11-driver
  90. pkcs11-rev
  91. portaudio_c_bindings
  92. portmidi
  93. posix-base
  94. posix-bindings
  95. posix-getopt >= "2.0.0"
  96. posix-signal
  97. posix-socket
  98. posix-socket-unix
  99. posix-time2
  100. posix-types
  101. posix-uname
  102. ppx_cstubs >= "0.7.0"
  103. protocol-9p < "0.5.1"
  104. py >= "1.1"
  105. quickjs
  106. raygui
  107. raylib
  108. reed-solomon-erasure
  109. rfc1951 >= "1.3.0"
  110. sanlock
  111. sarek >= "20210823"
  112. sattools
  113. sodium >= "0.2.0" & < "0.3.0"
  114. spin >= "0.8.0"
  115. spoc >= "20170724"
  116. srt
  117. stk_iconv
  118. swipl
  119. sys-socket
  120. sys-socket-unix
  121. tensorflow
  122. tezos-hacl
  123. tezos-hacl-glue-unix < "11.0"
  124. tezos-sapling >= "11.1"
  125. tezos-wasmer
  126. tgls = "0.8.3"
  127. torch
  128. tsdl >= "0.8.1" & < "0.9.1" | >= "1.0.0"
  129. tsdl-image >= "0.3.0"
  130. tsdl-mixer
  131. tsdl-ttf >= "0.3"
  132. unix-errno >= "0.4.1" & < "0.5.0" | >= "0.6.2"
  133. unix-fcntl >= "0.3.3"
  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
OCaml

Innovation. Community. Security.