package ctypes

  1. Overview
  2. Docs
Combinators for binding to C libraries without writing any C

Install

Dune Dependency

Authors

Maintainers

Sources

0.21.1.tar.gz
sha256=6f2b3b0f26c202b75ed7df3867fd9580d3e592af4944875b92ec0ee3ca7e14ff
md5=8b201d932741c5096854e5eb39139b90

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: 21 Jul 2023

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

Dependencies (5)

  1. bigarray-compat
  2. dune-configurator
  3. integers
  4. ocaml >= "4.03.0" & < "5.2"
  5. dune >= "2.9"

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

Conflicts (1)

  1. host-system-msvc
OCaml

Innovation. Community. Security.