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
  3. arakoon >= "1.8.6" & < "1.8.12"
  4. arb
  5. argon2
  6. arrayjit
  7. async_ssl >= "v0.15.0"
  8. avroc
  9. bimage-unix
  10. bitgenerators
  11. calcium
  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. iocaml-kernel >= "0.4.4"
  41. kqueue < "0.2.0"
  42. libdash >= "0.3"
  43. libirmin
  44. libudev < "0.2.1"
  45. lilv
  46. llama-cpp-ocaml
  47. llvm >= "3.7"
  48. lp-glpk
  49. lp-gurobi
  50. luv
  51. luv_unix
  52. lz4 >= "1.3.0"
  53. mariadb >= "1.1.2" & < "1.1.5"
  54. memcpy >= "0.2.2"
  55. mmdb
  56. monocypher
  57. mpg123
  58. nanomsg
  59. nebula
  60. netlink >= "0.3.4"
  61. octez-l2-libs
  62. octez-libs
  63. opasswd >= "1.3.1"
  64. opencc < "transition"
  65. opencc0
  66. opencc1
  67. opencc1_1
  68. orocksdb
  69. osbx < "1.1.1"
  70. osx-acl
  71. osx-attr >= "0.2.0"
  72. osx-cf >= "0.1.1"
  73. osx-fsevents >= "0.1.1"
  74. osx-membership
  75. osx-mount >= "0.1.1"
  76. osx-secure-transport
  77. osx-xattr >= "0.3.0"
  78. owl >= "0.9.0"
  79. pari
  80. pari-bindings
  81. pci < "1.0.1"
  82. pg_query
  83. pkcs11-driver
  84. pkcs11-rev
  85. portaudio_c_bindings
  86. portmidi
  87. posix-base
  88. posix-bindings
  89. posix-getopt >= "2.0.0"
  90. posix-signal
  91. posix-socket
  92. posix-socket-unix
  93. posix-time2
  94. posix-types
  95. posix-uname
  96. ppx_cstubs >= "0.7.0"
  97. protocol-9p < "0.5.1"
  98. py >= "1.1"
  99. quickjs
  100. raygui
  101. raylib
  102. reed-solomon-erasure
  103. rfc1951 >= "1.3.0"
  104. sanlock
  105. sarek >= "20210823"
  106. sattools
  107. sodium >= "0.2.0" & < "0.3.0"
  108. spin >= "0.8.0"
  109. spoc >= "20170724"
  110. srt
  111. stk_iconv
  112. swipl
  113. sys-socket
  114. sys-socket-unix
  115. tensorflow
  116. tezos-hacl
  117. tezos-hacl-glue-unix < "11.0"
  118. tezos-sapling >= "11.1"
  119. tezos-wasmer
  120. tgls = "0.8.3"
  121. torch
  122. tsdl >= "0.8.1" & < "0.9.1" | >= "1.0.0"
  123. tsdl-image >= "0.3.0"
  124. tsdl-mixer
  125. tsdl-ttf >= "0.3"
  126. unix-errno >= "0.4.1" & < "0.5.0" | >= "0.6.2"
  127. unix-fcntl >= "0.3.3"
  128. unix-sys-resource
  129. unix-sys-stat
  130. unix-time
  131. unix-type-representations >= "0.1.1"
  132. wasmer
  133. wasmtime
  134. xxhash
  135. yaml
  136. yara
  137. yices2_bindings
  138. zstandard
  139. zstd >= "0.4"

Conflicts

None

OCaml

Innovation. Community. Security.