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

Conflicts

None