Permalink
Switch branches/tags
Nothing to show
Find file Copy path
76a7f3b Feb 25, 2017
72 lines (52 sloc) 1.51 KB

Racket AutoFFI

This is the Racket binding generator built on top of AutoFFI.

Installation

raco pkg install https://github.com/AutoFFI/racket-autoffi.git

Getting Started

This is a very basic example showing what AutoFFI is capable of doing.

Say we have the following program in plain C:

myprog.c

int add(int a, int b) {
  return a + b;
}

We can compile this program on OS X as follows:

$ clang -o myprog.dylib -shared myprog.c

This gives us a binary:

$ hexdump myprog.dylib
0000000 cf fa ed fe 07 00 00 01 03 00 00 00 06 00 00 00
0000010 0c 00 00 00 78 02 00 00 85 00 10 00 00 00 00 00
0000020 19 00 00 00 e8 00 00 00 5f 5f 54 45 58 54 00 00
0000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0000040 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...

The binary does not contain enough information to link to it. AutoFFI generates the missing type information. In order to do this, we need our AutoFFI tool:

$ affc myprog.c > myprog.json

Importing this binary in Racket now requires only two lines of code. No need to define complex FFI structures!

main.rkt

#lang racket
(require autoffi)
(require/foreign "myprog")
(add 1 2)

If we run this program, we get:

$ racket test.rkt
3

That's it! Programs can get much more complicated, of course. Any bugs should be posted in the issue tracker.