chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] return a float from foreign function


From: Zbigniew
Subject: Re: [Chicken-users] return a float from foreign function
Date: Mon, 28 Aug 2006 11:34:44 -0500

Hello,

This is not a problem with Chicken nor with your use of float as
return type (float means float in Chicken, not double).

The problem is that you didn't provide a C prototype for your external
function.  This is a C issue, as you will see if you write a simple C
program:

$ cat > test.c <<EOF
#include <stdio.h>
/* float baz(int); */  /* look ma, no prototype */
int main(void) { printf("baz 42: %f\n", baz(42)); }
EOF
$ gcc -o test test.c impl.c
$ ./test
$ baz 42: 0.000000

Now uncomment the prototype and it works correctly.

To fix in your case, add the following to the top of callit.scm.

#> float baz(int); <#

Under normal use, of course, you would use
#> #include <impl.h> #<

Or use foreign-lambda* and include the code inline.

On 8/27/06, Shawn Rutledge <address@hidden> wrote:
impl.c:

#include <math.h>

float baz(int i)
{
        return (((float)i) / 15.2);
}

callit.scm:

(define baz (foreign-lambda float "baz" int))
(display baz)
(display (baz 42))

csc callit.scm impl.c

./callit
#<procedure (baz a25)>42.0

Change impl.c to make sure it's being called at all:

#include <math.h>
#include <stdio.h>

float baz(int i)
{
        printf("baz given %d\n", i);
        return (((float)i) / 15.2);
}

./callit
#<procedure (baz a25)>baz given 42
13.0

This is with chicken 2.2 and I think 2.3 also has similar problems.  I
can try 2.41 when it gets done building.




reply via email to

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