ANNOUNCE: cffi 1.0a7

65 views
Skip to first unread message

Ashok

unread,
Sep 18, 2021, 7:54:14 AMSep 18
to
The cffi package permits calling C functions in shared libraries from
within Tcl scripts.

Download and release notes:
https://sourceforge.net/projects/magicsplat/files/cffi/
Docs: https://cffi.magicsplat.com
Repository: https://github.com/apnadkarni/tcl-cffi

/Ashok

Arjen Markus

unread,
Sep 20, 2021, 11:04:19 AMSep 20
to
I tried to get it to work with (C) arrays but I failed. Here is my code:

# chkinprod.tcl --
# Simpel test for cffi
#
set auto_path [concat . $auto_path]
package require cffi

::cffi::dyncall::Library create inprod cprod.dll
inprod function plus double {x double y double}
inprod function inprod double {x double[n] y double[n] n int}

puts "Next step ..."

puts "Result: [plus 1.0 2]"
puts "Result: [inprod {1.0 2.0 3.0} {1.0 2.0 3.0} 3]"

puts "Done"

And the C code:

/* cprod.c --
Simple experiment with C function to test cffi
*/
double plus( double x, double y ) {
return x+y;
}
double inprod( double *x, double *y, int n ) {
int i;
double sum;

sum = 0.0;

for ( i = 0; i < n; i ++ ) {
sum = sum + x[i]*y[i] ;
}
return sum;
}

Could you tell me what I am doing wrong? The script stops after "Next step ...".

Regards,

Arjen

Ashok

unread,
Sep 21, 2021, 1:47:03 AMSep 21
to
Hi Arjen,

This is a bug triggered by giving the function the same name as the
library object. Defining the function inprod then destroys the library
object command (also called inprod) causing the DLL to be unloaded. You
need to give them different names. For example,

% package require cffi
1.0a7
%
% ::cffi::dyncall::Library create inprodlib ../cprod.dll
::inprodlib
% inprodlib function plus double {x double y double}
% inprodlib function inprod double {x double[n] y double[n] n int}
% puts "Result: [inprod {1.0 2.0 3.0} {1.0 2.0 3.0} 3]"
Result: 14.0

Alternatively, you could use a different name for the function instead
of the library,

inprod function {inprod inprodfunc} double {x double[n] y double[n] n int}

and then use inprodfunc to call the function.

Thanks for the bug report, I'll add protection in cffi to guard against
this.

/Ashok

Arjen Markus

unread,
Sep 21, 2021, 2:39:08 AMSep 21
to
Somewhere, in the back of my mind I thought I should not be so unimaginative as to use the same name, but I was too lazy to listen to myself. When I changed the name for the library (seemed more to the point than changing the name of the function ;)), it worked as intended. Very nice :).

Regards,

Arjen

Uwe Klein

unread,
Sep 21, 2021, 5:13:34 AMSep 21
to
Am 21.09.21 um 08:39 schrieb Arjen Markus:
> Somewhere, in the back of my mind I thought I should not be so unimaginative as to use the same name, but I was too lazy to listen to myself.

ROFL! Wordy, but should add that to the tcl-oneliners on the wiki.


"When I am back before I return, please tell me" :-)

Uwe
Reply all
Reply to author
Forward
0 new messages