[Ecls-list] how to call external C functions? [cvs-dec30, OS X 10.1.2]
John B. Thiel
jbthiel at pobox.com
Sun Jan 6 15:27:01 UTC 2002
Test objective is create an image linked with an external C fac.o and
call the function from
the lisp image. eg (fac 20)
fac.o <- gcc -c fac.c
fac.c contains:
int fac (int n) { if (n <= 1) return 1; return n * fac(n-1); }
The developer documentation purports to describe this, but it is
insufficiently precise, and/or the interface is buggy. How about a
step-by-step howto demonstration. Here follows my simplest test case
illustration of the errors.
[1] defentry does not work at all from ecl interactive, and appears to
be undefined. (perhaps this is correct?)
> (defentry fac (int) (int fac))
The variable FAC is unbound.
Top level.
> (defentry)
The function DEFENTRY is undefined.
Top level.
[2] Put the above defentry in 'fac-caller.lsp' and try to compile it.
ecl -compile fac-caller.lsp -s
;;; Compiling fac-caller.lsp.
;;; Compiling (DEFENTRY FAC ...).
;;; The variable FAC is undefined.
;;; The compiler will assume this variable is a global.
;;; End of Pass 1.
;;; Calling the C compiler...
"gcc -g -O2 -Ddarwin -O -I/usr/local/lib/ecl//h -w -c fac-caller.c -o
fac-caller.o"fac-caller.h:21: illegal external declaration, missing `;'
after `init_FAC'
fac-caller.h:21: illegal method definition, found `('
cpp-precomp: warning: errors during smart preprocessing, retrying in
basic mode
In file included from fac-caller.c:3:
fac-caller.h:21: parse error before `-'
fac-caller.h:21: method definition not in class context
(SYSTEM "gcc -g -O2 -Ddarwin -O -I/usr/local/lib/ecl//h -w -c
fac-caller.c -o fac-caller.o") returned non-zero value 1
[3] I have also tried first using c:build-program with :ld-flags to
build an image linking in the fac.o
-- that produced a startable image (thanks Juanjo) but defentry
still failed similarly and I still cannot
access the (fac) function.
[4] I tried using (ffi:defentry fac (int) (int fac)) and also
c::defentry. These fail compilation with an infinite loop:
ecl -compile fac-caller.lsp -s
;;; Compiling fac-caller.lsp.
The FFI special form FFI:DEFENTRY cannot be used in the interpreter.
;;; Compiling (FFI:DEFENTRY FAC ...).
;;; The macro form (FFI:DEFENTRY ...) was not expanded successfully.
;;; You are recommended to compile again.
;;; The macro form (FFI:DEFENTRY ...) was not expanded successfully.
;;; You are recommended to compile again.
[repeats....]
Let me know if additional details, or the intermediate .c/.h/.data files
wiIl be helpful for debugging.
-jt
More information about the ecl-devel
mailing list