Xref: utzoo comp.lang.c:39467 comp.lang.lisp:4920 Newsgroups: comp.lang.c,comp.lang.lisp Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!barmar From: barmar@think.com (Barry Margolin) Subject: Re: Calling LISP from C (QUERY) Message-ID: <1991May22.180731.4991@Think.COM> Sender: news@Think.COM Reply-To: barmar@think.com Organization: Thinking Machines Corporation, Cambridge MA, USA References: <1096@redford.UUCP> Date: Wed, 22 May 91 18:07:31 GMT [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