English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Binding C libraries which use variable arguments (stdarg.h)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-11-01 (09:31)
From: Adrien <camaradetux@g...>
Subject: Re: [Caml-list] Binding C libraries which use variable arguments (stdarg.h)
On 30/10/2009, Xavier Leroy <Xavier.Leroy@inria.fr> wrote:
> [Invoking a variadic C argument from OCaml]
>>> Assuming a Linux system, you could lazily generate the glue code and
>>> invoke dynamic linker on it. So the general case would be to call the
>>> code generator.
>> On linux and x86, I could probably use an array where I would move by
>> sizeof(some_type). I really want portability however (I want to use
>> these bindings at least on windows too).
> It might be worth investigating the "libffi" library.  If I guess
> correctly, it lets you synthesize calls to native C functions when the
> number and type of arguments are statically unknown.

I had checked libffi but its readme (from gcc-4.3) stated variable
arguments weren't supported. After some time I had doubts about the
relevance of the documentation (the last change mentionned was from
98) and tried anyway.

It turned out it worked on linux x86_32, linux x86_64, netbsd sparc64
with 32-bit userland, debian's kfreebsd amd64 and ppc32. There's
windows which I haven't checked yet though.

I posted my test code on vpaste.net[1]. I think it's good enough to
actually check libffi works with variable arguments but it could
probably be made better. Any comment welcome.

I also asked on the libffi mailing-list and was told by someone it
should work almost everywhere except on really exotic architectures.
That means I'll probably go the libffi way. :-)

[1] http://vpaste.net/6vPst?


Adrien Nader