yallop/ocaml-ctypes
Fork: 95 Star: 373 (更新于 2024-12-22 17:06:30)
license: MIT
Language: OCaml .
Library for binding to C libraries using pure OCaml
最后发布版本: 0.21.1 ( 2023-07-21 04:02:14)
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
- Building C libraries in OCaml with the dune ctypes stanza describes how to use ctypes with dune
- Chapter 19: Foreign Function Interface of Real World OCaml describes ctypes
- Modular Foreign Function Bindings introduces ctypes in the context of the Mirage library operating system
- Tutorial
- API documentation
- Mailing list
- Type-safe C bindings using ocaml-ctypes and stub generation introduces the Cstubs interface
-
Using Cstubs_structs shows how to use the
Cstubs_structs
module to reliably determine data layout - A modular foreign function interface is a research paper (extending an earlier paper) that presents the design of ctypes
- FAQ
最近版本更新:(数据更新于 2024-09-12 07:05:50)
2023-07-21 04:02:14 0.21.1
2023-07-13 03:06:47 0.21.0
2023-02-18 22:25:42 0.20.2
2022-03-31 19:15:21 0.20.1
2021-12-09 23:30:36 0.20.0
2021-06-26 15:48:57 0.19.1
2021-06-26 13:55:53 0.19.0
2020-11-12 23:06:30 0.18.0
2020-02-27 18:07:14 0.17.1
2020-02-23 01:27:57 0.17.0
yallop/ocaml-ctypes同语言 OCaml最近更新仓库
2024-09-10 20:49:27 c-cube/ocaml-containers
2024-09-04 16:25:47 mirleft/ocaml-tls
2024-06-29 16:58:29 ocaml/ocamlbuild
2024-06-17 17:04:46 ocaml/dune
2024-05-26 22:10:33 ocsigen/js_of_ocaml
2024-05-13 20:27:08 ocaml/ocaml