[racket] FFI

From: Jay Kominek (kominek at gmail.com)
Date: Wed May 15 15:28:14 EDT 2013

On Mon, May 13, 2013 at 2:58 AM, Kubilay Kaan <heraklea at gmx.de> wrote:
> [ ... C++ code omitted ...]
> So, this is the Code behind my dll. And I would bind this MyObj to a racket
> object. How can I achieve this?

I was afraid you were trying to use C++ when you mentioned objects.

Unfortunately the ABI for C++ isn't standardized, and different
compilers do different things. That makes it challenging to even
figure out what symbol to use for functions. libffi, which Racket uses
for making foreign function calls, doesn't have any support for C++.

You've got a number of options, none of them particularly appealing:

1) Rewrite the C++ as C. You can always do "pseudo" objects in C. (See gtk)

2) Write a C wrapper for the C++. You can compile it alongside the
C++, so long as you indicate you want C linkage for the relevant
functions. See:
for some explanation

3) I believe SWIG can generate wrappers for C++ libraries. http://www.swig.org/

4) If you know you only need to deal with the library as compiled by a
single compiler, you could inspect the DLLs produced, figure out the
symbols, and invoke them as though they're C functions. That might
work; I'm not sure what of the ramifications would be. The Haskell
wiki discusses doing this in Haskell:

For more background than you'll probably want, Agner Fog has a
document on the various C++ ABIs: http://www.agner.org/optimize/

Jay Kominek

Posted on the users mailing list.