• ANNOUNCE: cffi 1.0a7

    From Ashok@21:1/5 to All on Sat Sep 18 17:24:10 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arjen Markus@21:1/5 to Ashok on Mon Sep 20 08:04:17 2021
    On Saturday, September 18, 2021 at 1:54:14 PM UTC+2, Ashok wrote:
    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
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ashok@21:1/5 to Arjen Markus on Tue Sep 21 11:16:57 2021
    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 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
    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



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arjen Markus@21:1/5 to Ashok on Mon Sep 20 23:39:06 2021
    On Tuesday, September 21, 2021 at 7:47:03 AM UTC+2, Ashok wrote:
    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 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
    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


    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Uwe Klein@21:1/5 to All on Tue Sep 21 11:13:31 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)