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

Conflicts

None