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

Conflicts

None