From: Michael Hannemann <hannemann@in...> - 2003-03-31 23:20:03
From looking about on the web, I suspect this question has been asked a
hundred times, but I don't see a hundred answers, so I'll be brave and ask
How do I use my statically built library in an external C program?
The goal of the entire operation is to be able to give someone what looks
like a standard (shared or static) C library so that they don't have to
worry about there being Lisp inside.
Following the steps in the developer's guide, it seems like I should be
quite close. I've been able to copy the stand-alone executable to another
system and watch it run. (2.5MB! Much smaller than ACL or
Lispworks.) I've produced a .a file, and I've looked at the temporary .c
and .h files. But it seems like there's crucial interface information
missing; I suspect I could call the munged and re-defined C functions L1
(L2, &c.), but is there an easier way?
My sample Lisp file is simple:
(defun fact (n)
"A simple factorial function."
(if (<= n 1)
(* n (fact (- n 1)))))
(defun fib (n)
"A lame Fibonacci function."
(if (<= n 1)
(+ (fib (- n 1))
(fib (- n 2)))))
I can inspect the temporary header file, the C source, and all that. But
I'm not sure how to use it all. Given this toy example, would it be
possible for someone to show me how to use this library I've built to call
these Lisp-defined functions from a C program?
Senior Programmer, I/NET Inc.
From: Juan Jose Garcia Ripoll <worm@ar...> - 2003-04-01 07:51:32
On Tuesday 01 April 2003 01:18, Michael Hannemann wrote:
> Following the steps in the developer's guide, it seems like I should be
> quite close. I've been able to copy the stand-alone executable to anot=
> system and watch it run. (2.5MB! Much smaller than ACL or
> Lispworks.) I've produced a .a file, and I've looked at the temporary =
> and .h files. But it seems like there's crucial interface information
> missing; I suspect I could call the munged and re-defined C functions L=
> (L2, &c.), but is there an easier way?
Have you considered using cl_funcall(), cl_apply() and c_string_to_object=
With s =3D c_string_to_object("FIB") you obtain the symbol for your funct=
(assuming that the library has been initialized before). With cl_funcall(=
s, MAKE_FIXNUM(2)), you use your function.
The only problem is initialization. Lisp libraries have to be initialized=
an order which corresponds to the order in which the would be loaded if t=
were *.lsp files. ECL takes care of this when building executable program=
build-program produces the right sequence of C statements for that. If yo=
not want to use ECL to link the files, then I am afraid you will have to=20
inspect the output of build-program and reproduce it yourself.
Sorry, I know the interface is quite ugly, but I am very busy fixing erro=
the lisp part :-)