Path: utzoo!utgpu!attcan!uunet!pilchuck!dataio!uw-entropy!uw-june!whaley From: whaley@june.cs.washington.edu (Ken Whaley) Newsgroups: comp.lang.lisp Subject: Re: foreign functions to KCL Summary: Good news and Bad news. Message-ID: <6381@june.cs.washington.edu> Date: 10 Nov 88 06:08:38 GMT References: <5363@medusa.cs.purdue.edu> Reply-To: whaley@uw-june.UUCP (Ken Whaley) Organization: U of Washington, Computer Science, Seattle Lines: 62 The good news is that it's easy to incorporate C functions into KCL. The bad news is that unless you want to write C code that can't be used outside of the KCL environment, then you are constrained to use certain input parameter and function return types. I don't purport to be an expert on KCL -- this is just what I figured out from playing around with the system. By the way, I'm working with the "June 3, 1987" version of KCL. As an example of how to use C code from KCL, let's write the square function (that takes an integer argument and returns its square) in C, and foreign function it into KCL. Let's call the C function "c_square", and the LISP function "square". :NOTE: The KCL documentation says that the following is specific to the BSD version of KCL. 1. Create a file called "square.lsp" that consists of the line: (defentry square (int) (int c_square)) Argument 1 type------------^ Return type----------------------^ Name of C function in C source file-----^ (Other types that I know work are: char, double. I imagine that any "one-work" type should work. 2. Create the C file, say c_square.c: int c_square (x) int x; { return (x * x); } 3. In KCL, type (compile-file "square"). 4. Compile the C file from the shell (cc -c -O c_square.c) 5. In KCL, now type (si:faslink "square.o" "c_square.o") 6. You're in business. (square 7) ==> 49 Note that the second string in step 5 is passed directly to "ld", so you can include other libraries (I don't believe that the C library is automatically included for you, so if you use any C library routines, you'll have to write the following for step 5): (si:faslink "square.o" "c_square.o -lc"). BTW, this information in included in the "doc/kclunix" file of the KCL distribution. Oh, and I've only managed to get this working with non-pointer types. I haven't experimented with structs, either. You can pass in, manipulate, and return arbitrary LISP objects (KCL itself is written in C....), but that sort of information is, as far as I know, undocumented, and you'd have to dig through the source code to figure it out. Ken -- whaley@june.cs.washington.edu