[Ecls-list] Binding C/C++ Functions From Calling C/C++ Code

Juan Jose Garcia-Ripoll juanjose.garciaripoll at googlemail.com
Mon Feb 23 09:26:28 UTC 2009

On Thu, Feb 19, 2009 at 6:58 AM, Nikhilesh S <s.nikhilesh at gmail.com> wrote:
> I wanted to ask whether there's any function in the ECL C library to
> bind a C function from a program to a Lisp function. [...]
> cl_bind(&add, "c-add", /*min params*/ 1, /*max params*/ 1);

Hi, and first of all sorry about the late answer. ECL cannot bind
arbitrary C functions to lisp symbols. The reason is pretty obvious:
Lisp functions take and return lisp objects and have pretty well
defined and limited calling conventions. C/C++ functions do not.

There are several approaches to what you want to do.

1) Write a Lisp wrapper that follows our calling conventions. Something like

cl_object lisp_add(cl_object a, cl_object b) {
   cl_fixnum fa = fix(a);
   cl_fixnum fb = fix(b);

2) Write the previous code but in Lisp and compile it

(defun add (a b)
  (ffi:c-inline (a b) (:int :int) :int "#0+#1" :one-liner t))

3) Use a foreign function interface to link your C function with lisp.
This basically automates the previous two steps. It is explained in
the manual.


Instituto de Física Fundamental, CSIC
c/ Serrano, 113b, Madrid 28009 (Spain)

More information about the ecl-devel mailing list