• Stars
    star
    344
  • Rank 123,066 (Top 3 %)
  • Language
    OCaml
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Library for binding to C libraries using pure OCaml

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.

Links

More Repositories

1

effects-bibliography

A collaborative bibliography of work related to the theory and practice of computational effects
941
star
2

higher

Higher-kinded programming in OCaml
OCaml
176
star
3

icfp2014-papers

Accepted papers for ICFP 2014
121
star
4

ocaml-asp

Algebraic, staged parsing for OCaml: typed, compositional, and faster than yacc
OCaml
88
star
5

popl2015-papers

Accepted papers for POPL 2015
68
star
6

fomega

System Fω interpreter for use in Advanced Functional Programming course
OCaml
64
star
7

ocaml-integers

Various signed and unsigned integer types for OCaml
OCaml
57
star
8

haskell2014-papers

Accepted papers for Haskell 2014
52
star
9

ocaml-re-nfa

OCaml code to construct an NFA from a regular expression
OCaml
47
star
10

ocaml-ctypes-inverted-stubs-example

Example showing how to turn OCaml code (here Xmlm) into a C library.
OCaml
40
star
11

ocaml-flap

A deterministic parser with fused lexing
OCaml
34
star
12

ocaml-pb

An OCaml library for describing Protobuf messages
OCaml
30
star
13

staged-generic-programming

Code for "Staged Generic Programming" (ICFP 2017)
OCaml
26
star
14

ocaml-charset

Fast char sets
OCaml
20
star
15

ocaml-memcpy

OCaml
20
star
16

metaocaml-letrec

flexible 'let rec' generation for MetaOCaml
OCaml
19
star
17

ocaml-syb

Scrap Your Boilerplate for OCaml with modular implicits
OCaml
18
star
18

metaocaml-syb

Scrap Your Boilerplate for MetaOCaml with modular implicits
OCaml
18
star
19

ocaml-pb-plugin

A protoc plugin for generating OCaml code from protobuf (.proto) files.
OCaml
17
star
20

pldi2014-papers

Accepted papers for PLDI 2014
16
star
21

ocaml-safe-string-buffer

A buffer implementation optimized for immutable strings.
OCaml
14
star
22

ocaml-bindings-generator

OCaml
9
star
23

causal-commutative-arrows-revisited

Causal commutative arrows revisited: an optimizing arrow library
Haskell
9
star
24

metaocaml-charmatch

OCaml
8
star
25

ocaml-posix-types

ctypes-compatible type representations for the types exposed in <sys/types.h>
OCaml
8
star
26

ocaml-unix-type-representations

Functions that expose the underlying types of some abstract types in the Unix module.
OCaml
8
star
27

ocaml-reex

Staged regular expression library for MetaOCaml
OCaml
7
star
28

generalized-partial-computation-bibliography

Generalized Partial Computation Bibliography
7
star
29

code-snippets

miscellanea
OCaml
6
star
30

ocaml-ctypes-build

OCaml
5
star
31

ocaml-ctypes-foreign-example

Example showing how to bind to a C library (here libncurses) dynamically using ocaml-ctypes.
OCaml
5
star
32

gaussian-elimination

OCaml
4
star
33

irmin-inverted-stubs

OCaml
4
star
34

ocaml-ctypes-expat-example

OCaml
3
star
35

intrinsics-report

OCaml
3
star
36

advanced-fp-repo

OPAM repository for the Advanced Functional Programming course
2
star
37

ocaml-patterns

Automatically exported from code.google.com/p/ocaml-patterns
2
star
38

ocaml-java-types

ocaml-java-types
OCaml
2
star
39

check-markdown-urls

Python
1
star
40

ocaml-windows-segfault

OCaml
1
star
41

ocaml-ctypes-skeleton

Makefile
1
star