The cffi package permits calling C functions in shared libraries fromI tried to get it to work with (C) arrays but I failed. Here is my code:
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
On Saturday, September 18, 2021 at 1:54:14 PM UTC+2, Ashok wrote:
The cffi package permits calling C functions in shared libraries fromI tried to get it to work with (C) arrays but I failed. Here is my code:
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
# 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
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
On 9/20/2021 8:34 PM, Arjen Markus wrote:
On Saturday, September 18, 2021 at 1:54:14 PM UTC+2, Ashok wrote:
The cffi package permits calling C functions in shared libraries fromI tried to get it to work with (C) arrays but I failed. Here is my code:
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
# 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
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 285 |
Nodes: | 16 (2 / 14) |
Uptime: | 21:48:27 |
Calls: | 6,448 |
Files: | 12,050 |
Messages: | 5,254,047 |