[plt-scheme] foreign and C macros

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Mar 28 19:32:10 EST 2005

On Mar 28, David Van Horn wrote:
> I was recently trying to rewrite my MySQL library to use foreign.ss
> instead of cffi.ss.  Unfortunately the MySQL C API uses macros in
> place of procedures for many of the library's functionalities.
> Eg: #define mysql_num_rows(res) (res)->row_count
> [...]
> So is there a graceful solution to this problem using foreign.ss?

Well, not really.  In the long run, it might be feasible to write a C
header parser (Joe has something that's already working, but more work
is required to actually use it), but for now there is no simple

> Is there a not-so-graceful solution?  ie, how do I write ->row_count
> using foreign.ss?

One option is if you write a small C file that uses these macros, for

  int mysql_num_rows_func(Whatever *res) { return mysql_num_rows(res); }

compile that into a .so/.dylib/.dll, and then pull these functions out
using the foreign interface.  It does lose some flexibility since you
need some C, but that's much easier than writing a whole new C
extension, and the complex stuff is still done in Scheme.

> Do I have to duplicate the entire C typedef definition in Scheme?

That's the common thing I do -- see examples in the `ffi' collection.
It's pretty simple.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.