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. arb < "0.4.0"
  4. argon2
  5. arrayjit < "0.4.1"
  6. async_ssl >= "v0.15.0"
  7. bimage-unix
  8. bitgenerators
  9. calcium < "0.4.0"
  10. ceph
  11. cf
  12. checked_oint < "0.4.1"
  13. cmark
  14. cmarker
  15. ctypes-build
  16. ctypes-foreign = "0.21.1"
  17. ctypes-zarith
  18. ctypes_stubs_js
  19. cudajit
  20. decompress >= "1.3.0" & < "1.5.3"
  21. directories
  22. dlm >= "0.3.3"
  23. dune >= "3.17.2"
  24. echo
  25. eigen >= "0.1.4"
  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. hacl-star-raw >= "0.7.2"
  36. hardcaml_c
  37. hardcaml_verilator
  38. hdr_histogram
  39. imguiml
  40. kqueue < "0.2.0"
  41. libdash >= "0.3"
  42. libirmin
  43. lilv
  44. llama-cpp-ocaml
  45. llvm >= "3.7"
  46. lp-glpk
  47. lp-gurobi
  48. luv
  49. luv_unix
  50. lz4 >= "1.3.0"
  51. mariadb < "1.1.5" | >= "1.2.0"
  52. memcpy >= "0.2.2"
  53. mmdb
  54. mpg123
  55. netlink >= "0.3.4"
  56. nx
  57. octez-l2-libs
  58. octez-libs
  59. opasswd
  60. opencc < "transition"
  61. opencc0
  62. opencc1
  63. opencc1_1
  64. orocksdb
  65. osx-acl
  66. osx-attr
  67. osx-cf
  68. osx-fsevents
  69. osx-membership
  70. osx-mount
  71. osx-secure-transport
  72. osx-xattr
  73. owl >= "0.9.0"
  74. pari
  75. pari-bindings
  76. pg_query
  77. pkcs11-driver
  78. pkcs11-rev
  79. portaudio_c_bindings
  80. portmidi
  81. posix-base
  82. posix-bindings < "3"
  83. posix-getopt >= "2.0.0"
  84. posix-math2
  85. posix-signal
  86. posix-socket
  87. posix-socket-unix
  88. posix-time2
  89. posix-types
  90. posix-uname
  91. ppx_cstubs >= "0.7.0"
  92. py >= "1.1"
  93. quickjs
  94. raygui
  95. raylib
  96. reed-solomon-erasure
  97. rfc1951 >= "1.3.0"
  98. rune
  99. sanlock
  100. sarek
  101. sattools
  102. sodium < "0.6.0"
  103. spin >= "0.8.0"
  104. spoc
  105. srt
  106. stk_iconv
  107. swipl
  108. sys-socket
  109. sys-socket-unix
  110. tensorflow
  111. tezos-hacl
  112. tezos-hacl-glue-unix < "12.3"
  113. tezos-sapling
  114. tezos-wasmer
  115. tgls >= "0.9.0"
  116. torch
  117. tsdl >= "1.1.0"
  118. tsdl-image >= "0.3.0"
  119. tsdl-mixer
  120. tsdl-ttf >= "0.3"
  121. unix-errno >= "0.4.1" & < "0.5.0" | >= "0.6.2"
  122. unix-sys-resource
  123. unix-sys-stat
  124. unix-time
  125. unix-type-representations >= "0.1.1"
  126. uring-trace
  127. wasmer
  128. wasmtime
  129. xedbindings
  130. xxhash
  131. yaml
  132. yara
  133. yices2_bindings
  134. zstandard
  135. zstd >= "0.4"

Conflicts (1)

  1. host-system-msvc
OCaml

Innovation. Community. Security.