[Scilab-Dev] how to access to a compiled function in a C interface

Serge Steer Serge.Steer at scilab.org
Tue Aug 19 11:20:13 CEST 2008


Hello.

I give you the  basic elements  relative to scilab5 version.

Once  a code has been dynamically linked with Scilab, the associated
entry point address can be retreived using the
int SearchInDynLinks(char *op, void (**realop) ()) function (see
dynamic_link/src/c/dynamic_link.c)
given the entry point name in op it returns in *realop  a pointer on the
code entry point (The entry point name is those indicated by the
ilib_for_link first argument)

The linked function can then be called by
(** realop)(<arguments>);

For a more concrete example you can see the
optimization/src/c/optimtable.c code which correspond to the way optim
works:
- at the beginining of the optim gateway setfoptim is called and the
pointer on the linked function is memorized to avoid looking for it at
each call, this pointer is used to define the  function foptim (needed
because the optim code is written in fortran).
FTab_foptim and AddFunctionInTable are used to allow predefined codes
(codes which are included in the scilab build)

Serge Steer


Collette Yann a écrit :
> Hello,
>
> I would like to access to a compiled function from a C interface.
> Here is the intersting part of the optim documentation:
>
> C=['#include <math.h>'
>    'double sq(double x)'
>    '{ return x*x;}'
>    'void rosenc(int *ind, int *n, double *x, double *f, double *g, '
>    '                                int *ti, float *tr, double *td)'
>    '{'
>    '  double p;'
>    '  int i;'
>    '  p=td[0];'
>    '  if (*ind==2||*ind==4) {'
>    '    *f=1.0;'
>    '    for (i=1;i<*n;i++)'
>    '      *f+=p*sq(x[i]-sq(x[i-1]))+sq(1.0-x[i]);'
>    '  }'
>    '  if (*ind==3||*ind==4) {'
>    '    g[0]=-4.0*p*(x[1]-sq(x[0]))*x[0];'
>    '    for (i=1;i<*n-1;i++)'
>    '     
> g[i]=2.0*p*(x[i]-sq(x[i-1]))-4.0*p*(x[i+1]-sq(x[i]))*x[i]-2.0*(1.0-x[i]);'
>
>    '    g[*n-1]=2.0*p*(x[*n-1]-sq(x[*n-2]))-2.0*(1.0-x[*n-1]);'
>    '  }'
>    '}'];
>    mputl(C,TMPDIR+'/rosenc.c')
>    // compile the C code
>    l=ilib_for_link('rosenc','rosenc.o',[],'c',TMPDIR+'/Makefile');
>    // incremental linking
>    link(l,'rosenc','c');
>
> Once this function is compiled and linked, the 'rosenc' doesn't
> appears as a variable. I am only able to use this function via a call
> to fort.
> Where is this function in scilab ?
> How to I get a pointer to this function in a C interface ?
>
> YC
>




More information about the dev mailing list