From: Barry Margolin
Subject: Re: Calling LISP from C (QUERY)
Date: 
Message-ID: <1991May22.180731.4991@Think.COM>
[I replaced comp.lang.functional with comp.lang.lisp, which is more
appropriate.]

In article <····@redford.UUCP> ····@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.

······@think.com
{uunet,harvard}!think!barmar
From: Ted Dunning
Subject: Re: Calling LISP from C (QUERY)
Date: 
Message-ID: <TED.91May22161059@kythera.nmsu.edu>
In article <·····················@Think.COM> ······@think.com (Barry Margolin) writes:

   In article <····@redford.UUCP> ····@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:

	...

   What you generally *can* do is load C object files into a running Lisp.


it is often simpler to connect a c program an a lisp program with
something like a pair of pipes, or a socket.  then you can write code
which allows the either side to (effectively) call procedures on the
other side.  this sort of home brew remote procedure is often much
more portable between lisps and c programming environments than any
foreign function interface.

an additional advantage is that the debugging of each side is not
disturbed.  it is much harder to debug c code when it is loaded into a
lisp image.

--

if feeling bad is a justification for not living, 
    then living is a justification for feeling good.