[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Gcl-devel] foreign function call
From: |
Camm Maguire |
Subject: |
Re: [Gcl-devel] foreign function call |
Date: |
28 Sep 2004 11:39:02 -0400 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 |
Greetings, and please excuse the delayed reply!
This should be quite straightforward in GCL, providing one recognizes
that standard C has no multiple value return type, so that one must do
this part from lisp.
Here are a few examples which should illustrate the basic machinery:
clines, defentry, and defcfun. You can also check the gcl-si docs
here.
=============================================================================
/tmp/f.l
=============================================================================
(clines "
static double
square(double x) {
return x*x;
}
")
(defentry square (double) (double square))
(clines "
static double
first_value(double x,double y) {
return x+y;
}
")
(defentry first-value (double double) (double first_value))
(clines "
static double
second_value(double x,double y) {
return x-y;
}
")
(defentry second-value (double double) (double second_value))
(defun foo (x y) (values (first-value x y) (second-value x y)))
(defCfun "static object two_object_list(object x,object y)"
0
"object z,f,s;"
((+ x y) f)
((- x y) s)
((list f s) z)
"return z;")
(defentry two-object-list (object object) (object two_object_list))
(defun bar (x y) (let ((l (two-object-list x y))) (values (first l) (second
l))))
=============================================================================
Top level.
>(compile-file "/tmp/f.l")
Compiling /tmp/f.l.
End of Pass 1.
End of Pass 2.
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
Finished compiling /tmp/f.l.
#p"/tmp/f.o"
>(load "/tmp/f.o")
Loading /tmp/f.o
start address -T 0x83ec830 Finished loading /tmp/f.o
964
>(bar 1.2 4.3)
5.5
-3.0999999999999996
>(foo 1.2 4.3)
5.5
-3.0999999999999996
>
=============================================================================
Not sure about C vs. C++ issues, but my understanding is that the
latter is backward compatible with the former.
One can also reference code in external C libraries -- see the docs
for compiler::link in this case.
Please feel free to write back if questions persist.
Take care,
Marcio Fuzeto Gameiro <address@hidden> writes:
> I would like to know how (if possible) I can call a C++ function from
> Lisp? I need to be able to pass some values (floats/doubles) to the
> C++ function, and the function should return multiple values (like two
> doubles for example). Does any one now how to do this?
>
> Thanks,
>
> Marcio.
>
>
> _______________________________________________
> Gcl-devel mailing list
> address@hidden
> http://lists.gnu.org/mailman/listinfo/gcl-devel
>
>
>
--
Camm Maguire address@hidden
==========================================================================
"The earth is but one country, and mankind its citizens." -- Baha'u'llah