Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!sdd.hp.com!think.com!barmar From: barmar@think.com (Barry Margolin) Newsgroups: comp.lang.lisp Subject: Re: call C proceudre from Lisp Message-ID: <1991Apr25.045755.14098@Think.COM> Date: 25 Apr 91 04:57:55 GMT References: Sender: news@Think.COM Distribution: comp.lang.lisp Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 38 In article tl@tenacity.cs.uchicago.edu (Teng Li) writes: >Could anyone over there tell me how to include C subroutines which >would be written in a file into a Lisp program and how to call them >from Common Lisp? I need to do system call to excute some shell >commands, thus I have to write C code to deal with it.(Is that >right?) And my routines would be called by Lisp program. Do I need >some interfaces to make the C routines be recognized by Lisp? There's no standard mechanism for this. However, most Lisp systems include a facility called "foreign function calling" that supports this. In general, you use an implementation-dependent macro to define a Lisp function that will invoke the C function, properly converting the arguments. For instance, in Lucid Common Lisp, you would write something like this: (def-foreign-function (lisp-function (:language :c) (:name "_c_function_name") (:return-type :signed-32bit)) (int-arg :signed-16bit) (string-arg :string)) This defines a Lisp function named LISP-FUNCTION that invokes a C function named c_function_name, which would be prototyped as int c_function_name (short, char*); You also have to link the object file that contains the C function into your Lisp process. In Lucid, LOAD-FOREIGN-OBJECTS and LOAD-FOREIGN-LIBRARY are used to do this. As I said, the precise syntax differs from implementation to implementation, so you'll have to check your documentation for the specifics. -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar