Calling LISP from C (QUERY)

Barry Margolin barmar at think.com
Thu May 23 04:07:31 AEST 1991


[I replaced comp.lang.functional with comp.lang.lisp, which is more
appropriate.]

In article <1096 at redford.UUCP> bill at redford.UUCP (Bill Poitras(X258)) writes:
>I am trying to take some common LISP code which has some very complex
>algorithms and interface them to C.  The specifics:
>
>LISP: compiled common LISP
>OS: IRIX Unix System V, AIX 3.1.x , Sun OS 4.1
>Machine: Silicon Graphics Personal Iris, IBM RS/6000, Sun 4, Sun 3
>C compiler: C compiler which comes with the system.  ANSI C for RS/6000,
>K&R for Sun and IRIS.
>
>The type of calls that I would make:
>
>main()
>{
>	Molecule x;
>	int i;
>
>	i = LISP_Molecular_weight_of(x);
>}
>
>
>All of the computation would be in LISP, the User Interface for the
>program would be in C.  Hopefully this is enough.  If not, let me know.

You still haven't said what Lisp implementations you are using.  There are
several vendors of Common Lisp for most of the machines you mentioned.

Most Lisp compilers don't generate binaries that can be linked into other
programs.  The binaries that they generate are designed to be loaded into a
running Lisp.  Most Unix Lisp implementation provide a way to save a
running Lisp into an executable file (usually a function with a name like
DISKSAVE or DUMP), but this file is normally fully linked and has its own
main() entrypoint.

What you generally *can* do is load C object files into a running Lisp.
You can define a Lisp function that serves as an interface to a C function,
to allow calling C from Lisp.  In you case, when the Lisp image starts up,
it would immediately call the user interface routine, which is in C.  You
can also specify that a particular Lisp function should stand for a C
function, so that C can call back into Lisp.  This would be used in your
case for calling LISP_Molecular_weight_of().  The declarations are
necessary to cause data type translation of arguments take place.

Look in the documentation for your Lisp implementation for "foreign
functions".
-- 
Barry Margolin, Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list