package ctypes

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

Install

Dune 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

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. 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.0"
  26. eigen >= "0.1.4"
  27. extism
  28. flint
  29. fsevents
  30. gccjit
  31. gdal >= "0.10.0"
  32. gobject-introspection
  33. gpiod
  34. gr
  35. guile
  36. hacl-star-raw >= "0.7.2"
  37. hardcaml_c
  38. hardcaml_verilator
  39. hdr_histogram
  40. imguiml
  41. kqueue < "0.2.0"
  42. libbpf
  43. libbpf_maps
  44. libdash >= "0.3"
  45. libirmin
  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.5" | >= "1.2.0"
  55. memcpy >= "0.2.2"
  56. metal
  57. mmdb
  58. mpg123
  59. netlink >= "0.3.4"
  60. octez-l2-libs
  61. octez-libs
  62. opasswd
  63. opencc < "transition"
  64. opencc0
  65. opencc1
  66. opencc1_1
  67. orocksdb
  68. osx-acl
  69. osx-attr
  70. osx-cf
  71. osx-fsevents
  72. osx-membership
  73. osx-mount
  74. osx-secure-transport
  75. osx-xattr
  76. owl >= "0.9.0"
  77. pari
  78. pari-bindings
  79. pg_query
  80. pkcs11-driver
  81. pkcs11-rev
  82. portaudio_c_bindings
  83. portmidi
  84. posix-base
  85. posix-bindings < "3"
  86. posix-getopt >= "2.0.0"
  87. posix-math2
  88. posix-signal
  89. posix-socket
  90. posix-socket-unix
  91. posix-time2
  92. posix-types
  93. posix-uname
  94. ppx_cstubs >= "0.7.0"
  95. py >= "1.1"
  96. quickjs
  97. raygui
  98. raylib
  99. reed-solomon-erasure
  100. rfc1951 >= "1.3.0"
  101. sanlock
  102. sarek
  103. sattools
  104. sodium < "0.6.0"
  105. spin >= "0.8.0"
  106. spoc
  107. srt
  108. stk_iconv
  109. swipl
  110. sys-socket
  111. sys-socket-unix
  112. tensorflow
  113. tezos-hacl
  114. tezos-hacl-glue-unix < "12.3"
  115. tezos-sapling
  116. tezos-wasmer
  117. torch
  118. tsdl >= "1.0.0"
  119. tsdl-image >= "0.3.0"
  120. tsdl-mixer
  121. tsdl-ttf >= "0.3"
  122. unix-errno >= "0.4.1" & < "0.5.0" | >= "0.6.2"
  123. unix-sys-resource
  124. unix-sys-stat
  125. unix-time
  126. unix-type-representations >= "0.1.1"
  127. uring-trace
  128. wasmer
  129. wasmtime
  130. xedbindings
  131. xxhash
  132. yaml
  133. yara
  134. yices2_bindings
  135. zstandard
  136. zstd >= "0.4"

Conflicts (1)

  1. host-system-msvc
OCaml

Innovation. Community. Security.