Leslie Polzer has posted an excellent little example on Getting started with CFFI
However, I think it deserves mentioning that playing around with FFI definitions is a grand way of getting to know a C library. Unlike C, Lisp is an interactive programming environment which means that you can slowly build up the necessary chunks of code, trying out stuff in order to properly understand what the various bits and pieces does.
For this, 'getloadavg' (the example used in the aforementioned article) is actually too simple to properly relate to the power this gives you. Consider instead wanting to get to know something more involved such as OpenOBEX
(for talking to your mobile phone) or SAF
(for building highly available systems), then having an interactive environment is a tremendous benefit. Anybody who has tried to understand a complex C library by writing small snippets of C programs with printf's to demonstrate proper working should be able to appreciate the advantage of this.
In fact, I think that the interactivity of Lisp is one of the top five secrets of why Lisp programmers are so darn productive. Rather than building up large suites of tests, we just try out constructs as we write the code and the code is correct from the outset (well, sort of) rather than having to go through a couple of iterations to weed out the silly mistakes. If I didn't have the REPL nearby, I know I would never ever use something as complicated as reduce
, my head starts boiling everytime I have to construct even just a slightly complex 'reduce' expression, but being to able to iteratively build up and verify such expressions gives me the necessary confidence to stick it into my code anyway.
In case you think that writing the initial boilerplate for a C library interface, you should check out something like the Verrazano/Fetter
project. This is able to generate CFFI
bindings by parsing C header files. The result is not necessarily perfect but certainly a good start.
Verrazano is an old Google summer of Code of project and endured a slowdown but has again picked up steam and some of the problems in the initial implementation has been fixed in the curren instantiation; and I have found the developers to be very responsive on the mailing list when I reported some concerns. Highly Recommended (with a small salute to the Jerry Pournelle
columns in Byte Magazine
Current Mood: Da.., this is a good red wine