Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Creating wrappers for C libraries

2 views
Skip to first unread message

Erik de Castro Lopo

unread,
Dec 7, 2006, 11:57:17 PM12/7/06
to caml...@inria.fr
Hi all,

I've had a look at chapter 12 of the O'Rielly book as well as the
wrapper for ocaml-fftw3 and cairo-ocaml and it all seems a little
daunting.

I was wondering if there was any semi-automated too for creating
Ocaml bindings for C libraries.

Any pointers appreciated.

Cheers,
Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo
+-----------------------------------------------------------+
"Lumping configuration data, security data, kernel tuning parameters,
etc. into one monstrous fragile binary data structure is really dumb."
- David F. Skoll

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Chris King

unread,
Dec 8, 2006, 1:38:52 AM12/8/06
to Erik de Castro Lopo
On 12/7/06, Erik de Castro Lopo <mle+...@mega-nerd.com> wrote:
> I was wondering if there was any semi-automated too for creating
> Ocaml bindings for C libraries.

I recently discovered that SWIG (http://www.swig.org/) supports O'Caml
(though I haven't yet used it myself). If you're looking for speed
though it may not be the best choice since all C types are mapped to
one variant type in O'Caml. You can find O'Caml-specific docs here:
http://www.swig.org/Doc1.3/Ocaml.html

I presume you're looking to wrap libsndfile... I've wrapped a few
similar libraries manually with very few problems. The libsndfile API
looks to me like it should be fairly straightforward to wrap thanks to
O'Caml's Bigarrays (plus you'll have lots of cut & paste).

Start by writing a few simple integer, string, or array functions for
O'Caml in C to get a feel for the API without having to worry about
custom blocks and such. The curses wrapper in chapter 18 of the
O'Caml manual is also a very good starting point. The custom block
from that example can be used nearly verbatim for many C structures
(such as SNDFILE *). I can provide you with the source of a couple
other small wrappers if you like.

Hope this helps,
Chris

Sébastien Hinderer

unread,
Dec 8, 2006, 6:11:35 AM12/8/06
to david....@ens-lyon.org
Hi.

> One problem with such automated tools is that they have no clue of
> which C function takes time and which returns almost instantly.

Thanks a lot for this remark.

I'm also wondering how good automatic tools are for taking advantage of
Caml's rich typing system and of Caml features like exceptions.
An example will probably make my question more clear.
How would an automatic tool bind the open(2) function that returns either a
file descriptor (on success), or -1 to report failures, the precise reason
for the failure being contained in the errno variable. Would it be possible
to specify, with an automatic tool, that the returned valid is an int
unless open returns -1, in which case an error should be
reported through an exception ?

Or would an automatic tool bind open to a function returning an int in any
case, the task of testing the returned value having then to be performed
by the programmer, but in Caml ?

Sébastien.

Richard Jones

unread,
Dec 8, 2006, 6:44:22 AM12/8/06
to Erik de Castro Lopo
On Fri, Dec 08, 2006 at 03:54:37PM +1100, Erik de Castro Lopo wrote:
> I've had a look at chapter 12 of the O'Rielly book as well as the
> wrapper for ocaml-fftw3 and cairo-ocaml and it all seems a little
> daunting.
>
> I was wondering if there was any semi-automated too for creating
> Ocaml bindings for C libraries.

Obviously there are some semi-automated ways, such as SWIG and camlidl
mentioned already. But I would definitely urge you to look at doing
the wrapping by hand. It's really not that hard, although you should
pay careful attention to ch18 of the manual.

Rich.

ch18: http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html

--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com - NEW!

Daniel Bünzli

unread,
Dec 8, 2006, 7:05:27 AM12/8/06
to Erik de Castro Lopo
Another way is to meta program the bindings using C macros,
especially if many stubs have the same structure, have a look at the
file ml_gl.h in lablgl [1].

Daniel

[1] <http://wwwfun.kurims.kyoto-u.ac.jp/soft/lsl/lablgl.html>

0 new messages