[Openmcl-devel] Simple C FFI example for OS X?

Gary Byers gb at clozure.com
Tue Feb 3 07:38:42 PST 2004



On Mon, 2 Feb 2004, Peter Seibel wrote:

> Does anyone have a really trivial example of how to write some C code
> and call it from openmcl on OS X? I looked through the examples and it
> seemed that most of the OS X examples were Cocoa-based while the plain
> C examples were for LinuxPPC? I'm mostly interested in the mechanics
> of how to compile my C code so I can load it in Lisp and then call my
> functions. Like I said trivial stuff, but I'd rather cut-n-paste than
> figure it out from first principles if I can.
>
> FWIW, I'm working on hooking up to the QuickTime Music Architecture
> functions to play some music. My current theory is to write a thin
> layer in C that provides just the API I need in a way that will be
> convenient to call from Lisp and then do the rest up in Lisp.
> (Basically I need to be able to generate chunks of memory with
> QuickTime music events encoded in them and then stuff them into a
> TunePlayer and tell it to play them. No rocket science.)
>
> -Peter


Peter Seibel wrote:
> Does anyone have a really trivial example of how to write some C code
> and call it from openmcl on OS X? I looked through the examples and it
> seemed that most of the OS X examples were Cocoa-based while the plain
> C examples were for LinuxPPC? I'm mostly interested in the mechanics
> of how to compile my C code so I can load it in Lisp and then call my
> functions. Like I said trivial stuff, but I'd rather cut-n-paste than
> figure it out from first principles if I can.
>
> FWIW, I'm working on hooking up to the QuickTime Music Architecture
> functions to play some music. My current theory is to write a thin
> layer in C that provides just the API I need in a way that will be
> convenient to call from Lisp and then do the rest up in Lisp.
> (Basically I need to be able to generate chunks of memory with
> QuickTime music events encoded in them and then stuff them into a
> TunePlayer and tell it to play them. No rocket science.)
>
> -Peter
>

You're right that there should be examples of/pointers to something
that shows how to create a shared library (and, for OSX, there should
be a discussion of the different flavors of shared library available.)

Until a better example exists, here's something that might help to get
you started:

----- add2ints.c ------
/*
  Add 2 'int's together, return the result.
*/

int
add2ints(int x, int y)
{
  return x+y;
}
----- EOF ------

A better example would include a makefile.  Since this is so simple,
let's just ask the C compiler to create a "bundle"-type shared lib.
Since we aren't saying otherwise, all "extern" symbols will be exported
(with a leading underscore on OSX.)

gcc -o add2ints.so -bundle add2ints.c

;;; Load the shared library in lisp; use an absolute pathname in this
;;; case (since it's not installed in a canonical location):

? (ccl:open-shared-library "/Users/gb/add2ints.so")
#<SHLIB /Users/gb/add2ints.so #x546DACE>

;;; Verify that the external entry point "_add2ints" is known:
? (ccl:external "_add2ints")
#<EXTERNAL-ENTRY-POINT "_add2ints" (#x000CEFD0) /Users/gb/add2ints.so #x546D5E6>

;;; Pause a moment to wonder why, after a few years, CCL::EXTERNAL-CALL
;;; is still not exported.  Curse or shrug, then start adding:
? (ccl::external-call "_add2ints" :int 2 :int 2 :int)
4

----
Somewhere or other, there is (or is supposed to be) something that
lets you describe the syntax of foreign functions so that you'd be
able to use the #_ reader macro and not have to provide foreign
argument/result types:

(#_add2ints 2 2)


For things that're more substantial than this example, the
interface translator/interface database do that for you.  For things
that aren't much more substantial than this example, they're probably
overkill.

Somewhere on the Fink website (<http://fink.sourceforge.net>), there
is or was a very good explanation of the different flavors of
shared library available under OSX.

Hope that's trivial enough to get you started.

Gary Byers
gb at clozure.com



More information about the Openmcl-devel mailing list