chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] noobie question about chicken/swig/csi


From: minh thu
Subject: Re: [Chicken-users] noobie question about chicken/swig/csi
Date: Mon, 2 Apr 2007 11:00:47 +0200

2007/4/2, minh thu <address@hidden>:
2007/4/2, Tato Norren <address@hidden>:
> Hi,
> I feel like an idiot asking, but what exactly are the sequence of
> commands needed to make the following C function callable from the
> chicken interpretor? on Linux.
>
> //test.c
> double test(double x){
>     return x * x;
> }
>
> //test.i
> %module test
> %{
> double test(double);
> %}
>
> thanks,
> tato
>

Hi,

For simple things like that, don't use swig.
In a .scm file, write this:
(define test
  (foreign-lambda double "test" double))

'test' is the name in Scheme.
"test" is the name in C.
The first double is the return type.
The last double is the arg.

Compile the .scm and the .c files together with csc -s.
Note the -s whiwh will make a shared library : a .so file.
Now assuming the file were called tt.*, you can bring the function in csi with
csi -require-extension tt
or calling (require-extension tt) from a script.

For more complete explanation, see here:
http://chicken.wiki.br/Interface to external functions and variables

There is another way : just use the declaration of your function with
foreign-parse, it will automatically make it available in Scheme.

Use the Search Box and your left on
http://chicken.wiki.br/.

Cheers,
thu


Re,
I forgot to say that you need to declare your function.

Here an example:
address@hidden:~/projets/chicken/test$ cat test.c
/* test.c */

double
test( double x )
{
 return x * x;
}

address@hidden:~/projets/chicken/test$ cat tt.scm
; test.scm

; you can replace the following with a (foreign-parse ...) ....
#>
double test( double );
<#

; ... then you don't need this !
(define test
  (foreign-lambda double "test" double))
address@hidden:~/projets/chicken/test$ csc -s test.c tt.scm
address@hidden:~/projets/chicken/test$ csi -require-extension tt

     /    /      /
___ (___    ___ (     ___  ___
|    |   )| |    |___)|___)|   )
|__  |  / | |__  | \  |__  |  /

Version 2.6 - linux-unix-gnu-x86 - [ libffi dload ptables applyhook ]
(c)2000-2007 Felix L. Winkelmann
; loading /home/mt/.csirc ...
; loading /usr/local/lib/chicken/1/readline.so ...
; loading library regex ...
; loading ./tt.so ...
#;1> (test 5)

#;1>
#;1> (test 5)
25.0
#;2> ,q

Cheers,
thu




reply via email to

[Prev in Thread] Current Thread [Next in Thread]