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

Goffioul Michael goffioul at imec.be
Wed Jan 11 00:11:01 UTC 2006


Another possibility is to use dynamic FFI (available under x86
platform).
In that case, you don't need to compile your LISP code, but you have to
use
the :module argument of def-function, which can be a library name or
:default
(will look into the current executables and loaded libs).

Imagine you have a C function: int f1(int a, int b) defined somewhere in
your app. To make it available to LISP, you can use:

(ffi:def-function ("f1" f1) ((a :int) (b :int)) :returning :int :module
:default)

Michael.

> 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: 
> http://cvs.sourceforge.net/viewcvs.py/xchatlisp/eclplugin/plug
> in.c?rev=1.20&view=markup
> 
> eg. Xchat uses this:
> 
> cl_def_c_function(str_intern("%XCHAT-LIST-GET"),cl_xchat_list_get,1);
> 
> 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-FUNCTI
> ON"), (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.
> 
> Cheers
> Dean.
> 
> David Creelman wrote:
> 
> >Hi,
> >
> >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 :
> >int)
> >NUMBER-PLUS
> >  
> >
> >>(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.
> >
> >Cheers
> >DC
> >  
> >
> 
> 
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep 
> through log files for problems?  Stop!  Download the new AJAX 
> search engine that makes searching your log files as easy as 
> surfing the  web.  DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Ecls-list mailing list
> Ecls-list at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ecls-list
> 




More information about the ecl-devel mailing list