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

Conflicts (1)

  1. host-system-msvc
OCaml

Innovation. Community. Security.