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

Conflicts (1)

  1. host-system-msvc
OCaml

Innovation. Community. Security.