guile-user
[Top][All Lists]
Advanced

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

Re: FFI arg_types for C array


From: Nala Ginrut
Subject: Re: FFI arg_types for C array
Date: Thu, 12 Mar 2020 16:08:37 +0800

BTW, you may realize that the FFI type is platform-dependent, so u32vector
is assuming your platform is x86.
Please take care of these issues when you write product code.

Best regards.


On Thu, Mar 12, 2020 at 4:06 PM Nala Ginrut <address@hidden> wrote:

> Hi Myles!
> In your case, you've announced int array in your C code, so you shouldn't
> use u8vector, the correct one is (u32vector 0 1 2 3 4 5 6 7 8 9)
> Please notice that 0 is the placeholder since your array starts from 1.
>
> On Thu, Mar 12, 2020 at 4:22 AM Myles English <address@hidden>
> wrote:
>
>> Hi Nala,
>>
>> On Tue, 10 Mar 2020, 18:50 Nala Ginrut, <address@hidden> wrote:
>>
>>> If I understand your question correctly,  the arg_type should be (list
>>> int '*)
>>>
>>> On Tue, Mar 10, 2020 at 10:25 PM Myles English <address@hidden>
>>> wrote:
>>>
>> I am trying to call a C function with this signature from Guile:
>>>
>>>>
>>>> void the_test(int n, const int the_array[]);
>>>>
>>>> in pointer->procedure what should arg_types be?
>>>>
>>>
>> Thank you for reply, it does indeed answer my original question.  Now,
>> how do I call the guile procedure to use the C function?  I thought this
>> might work but it doesn't:
>>
>> In file main.scm:
>>
>> (use-modules (system foreign)
>>                          (rnrs bytevectors))
>>
>> (define libarray (dynamic-link "/path/to/libarray.so"))
>>
>> (define the-test
>>   (pointer->procedure void
>>     (dynamic-func "the_test" libarray)
>>     (list int '*)))
>>
>> (define (call-the-test)
>>   (let* ((bv  (u8-list->bytevector '(1 2 3 4 5 6 7 8 9)))
>> (bv_p (bytevector->pointer bv)))
>>     (display bv) (newline)
>>     (the-test 9 bv_p)))
>>
>> (call-the-test)
>>
>> In file array.c:
>>
>> #include <stdio.h>
>> void the_test(int n, const int the_array[])
>> {
>>   int i, k;
>>   for (k = 1; k <= n; k++)
>>     {
>>       i = the_array[k];
>>       printf("the_array [%d] = %d\n", k, i);
>>     }
>>   return;
>> }
>>
>> compiled with:
>>
>> gcc -c -Wall -fpic array.c && gcc -shared -o libarray.so array.o
>>
>> This is the result of calling the guile module, (I was hoping to get the
>> numbers 1 to 9):
>>
>> $ guile main.scm
>>     #vu8(1 2 3 4 5 6 7 8 9)
>> the_array [1] = 134678021
>> the_array [2] = 9
>> the_array [3] = 0
>> the_array [4] = 0
>> the_array [5] = 0
>> the_array [6] = 0
>> the_array [7] = 0
>> the_array [8] = 27
>> the_array [9] = 0
>>
>> In addition to the main question above, the C function expects a 1-based
>> index for the array, how would the guile procedure accommodate this?
>>
>> Thanks,
>> Myles
>>
>>>


reply via email to

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