package ctypes

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

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.23.0.tar.gz
sha256=cae47d815b27dd4c824a007f1145856044542fe2588d23a443ef4eefec360bf1
md5=b1af973ec9cf7867a63714e92df82f2a

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: 16 Aug 2024

README

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!

GitHub Actions status

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.

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

Conflicts (1)

  1. host-system-msvc