[Ecls-list] How do I call a C func from ECL when embedded in an app

Dean O'Connor dean.oconnor at ite.com.au
Tue Jan 10 23:55:01 UTC 2006

Hi David,

To my limited understanding, you need to compile your Lisp to use that 
ffi:def-function stuff. (I may be corrected here :)

We have a C++ app, with ECL embedded and we make our C functions 
available to the interpreted Lisp scripts by using ECL C-API function 
call "cl_def_c_function".

Probably one of my best sources of examples came from the XChatlisp 
plugin that uses ECL.
This C file has good stuff: 

eg. Xchat uses this:


I use similar, but used c_string_to_object instead of str_intern:

static cl_object
My_C_function(cl_object arg1, cl_object arg2)
	return Cnil;

cl_def_c_function((cl_lispunion*)c_string_to_object("MY-FUNCTION"), (cl_lispunion*)My_C_function, 2);

Note: I forget why I need to typecast to "cl_lispunion*", maybe that is 
not needed ?

There are examples in the ECLS CVS file: msvc\c\cinit.c
Also recently some examples of calling Lisp scripts from multiple C 
threads was added to CVS, into subdir examples\threads\import.
Note: The last arg of cl_def_c_function specifies how many args the 
declared function has. This should match the number of cl_object args of 
your C function.


David Creelman wrote:

>I'm trying to call a C function from ECL. ECL is embedded into my
>application (using WinXP).
>I looked through the mailing list and found something like the following
>as an example of how to use FFI, but I get an error:-
>>(ffi:def-function ("number_plus" number-plus) ((a :int)
>(b :int)) :returning :
>>(number-plus 1 2)
>The special form c-inline cannot be used in the interpreter.
>Broken at NUMBER-PLUS.
>I chose number-plus since it seemed most likely to be exported and
>visible within ECL.
>How do I define a function in C so that it's visible and callable from
>ECL ? Is this possible without compiling the LISP code ?
>Looked up the CFFI documentation, but it didn't seem to talk about how
>to export the functions into ECL.

More information about the ecl-devel mailing list