2007/4/2, minh thu <[EMAIL PROTECTED]>:
2007/4/2, Tato Norren <[EMAIL PROTECTED]>:
> 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:
[EMAIL PROTECTED]:~/projets/chicken/test$ cat test.c
/* test.c */

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

[EMAIL PROTECTED]:~/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))
[EMAIL PROTECTED]:~/projets/chicken/test$ csc -s test.c tt.scm
[EMAIL PROTECTED]:~/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


_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to