[comp.lang.c] Calling LISP from C

jwabik@shamash.UUCP (Jeff Wabik) (06/30/88)

I am posting this for a friend.  Please respond only via e/mail to:

	UUCP:      shamash!halcdc!tciaccio
	    or
	Internet:  tciaccio%halcdc@shamash.cdc.com
	    or
		   jwabik@shamash.cdc.com


Thanks ..  

	-Jeff


----------------------------------------------------------------------------

I have a need to invoke LISP functions from C on a UNIX workstation
and pass arguments/results between the LISP and C functions.  Using
Kyoto Common LISP or Allegro CL the reverse can be done, i.e. call C
functions from LISP but this is done by having the LISP process
running first.  How to call LISP functions from C ?  Any input would
be appreciated.

---
Jeff A. Wabik   INTERNET: jwabik@shamash.cdc.com
  ____  ____    UUCP: {rosevax,umn-cs,bungia,ems}!shamash!jwabik  
 / ___||___ \   
| |___  ___| |  Control Data Corporation - Better living through 64 bits.
 \____||____/  
		  	   Live long and program.

cox@ernie.Berkeley.EDU (Charles A. Cox) (07/09/88)

In article <6632@shamash.UUCP> jwabik@shamash.UUCP (Jeff Wabik) writes:
>
>I have a need to invoke LISP functions from C on a UNIX workstation
>and pass arguments/results between the LISP and C functions.  Using
>Kyoto Common LISP or Allegro CL the reverse can be done, i.e. call C
>functions from LISP but this is done by having the LISP process
>running first.  How to call LISP functions from C ?

With Franz Inc.'s Allegro CL's foreign function interface, you can
call LISP functions from C through the `call-back' facility
(documented in the User's Guide).  The LISP process still has to
be the main one since LISP functions need to run in LISP's runtime
environment.

	Charley Cox
	cox@renoir.Berkeley.EDU

bill@polygen.uucp (Bill Poitras) (05/16/91)

How do I call compiled common lisp from C?  I have a large chunk of LISP
which I need to call from C and get data back into C.  How is this
usually done?  Any help would greatly be appreciated.  E-Mail is
preferable, but posting is OK.

+-----------------+---------------------------+-----------------------------+
| Bill Poitras    | Polygen Corporation       | {princeton mit-eddie        |
|     (bill)      | Waltham, MA USA           |  bu sunne}!polygen!bill     |
|                 | FAX (617)890-8694         | bill@polygen.com            |
+-----------------+---------------------------+-----------------------------+

bill@polygen.uucp (Bill Poitras) (05/20/91)

Someone wrote me back about this saying:
>if you give any information about what lisp, what machine, what
>operating system and what c you are using, then somebody might be able
>to help.
>
>if you add in a little information about what you are trying to do,
>then even more people can help.

So I will.  

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.

+-----------------+---------------------------+-----------------------------+
| Bill Poitras    | Polygen Corporation       | {princeton mit-eddie        |
|     (bill)      | Waltham, MA USA           |  bu sunne}!polygen!bill     |
|                 | FAX (617)890-8694         | bill@polygen.com            |
+-----------------+---------------------------+-----------------------------+

barmar@think.com (Barry Margolin) (05/23/91)

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

In article <1096@redford.UUCP> bill@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@think.com
{uunet,harvard}!think!barmar

ted@nmsu.edu (Ted Dunning) (05/23/91)

In article <1991May22.180731.4991@Think.COM> barmar@think.com (Barry Margolin) writes:

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