Calling a fortran subroutine from Scilab :
A little example :
fsum.f. The core function/routine which contains the intelligence.
c ================================= subroutine fsum(a,b,c) c ================================= double precision a,b,c c = a + b end c =================================
- sci_fsum.c. The Scilab interface (basically the link between the core function and the scilab engine)
/**********************************************************/ #include "stack-c.h" /**********************************************************/ extern int F2C(fsum)(double *a,double *b,double *c); /**********************************************************/ int sci_fsum(char *fname) { int l1, m1, n1, l2, m2, n2, m3, n3,l3; double a,b,c; a=0; b=0; c=0; /* --> result = fortransum(3,8) /* check that we have only 2 parameters input */ /* check that we have only 1 parameters output */ CheckRhs(2,2) ; CheckLhs(1,1) ; /* get first parameter and put in 'a' */ GetRhsVar(1, "d", &m1, &n1, &l1); /* Note that this code is deprecated and should be replaced by API_Scilab */ // GetRhsVar(1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1); // In Scilab 5 a = *stk(l1); /* get second parameter and put in 'a' */ GetRhsVar(2, "d", &m2, &n2, &l2); /* Note that this code is deprecated and should be replaced by API_Scilab */ // GetRhsVar(2, MATRIX_OF_DOUBLE_DATATYPE, &m2, &n2, &l2);// In Scilab 5 b= *stk(l2) ; /* call fortran fsum subroutine */ F2C(fsum)(&a,&b,&c); /* create a variable on scilab's stack */ m3=1; n3=1; CreateVar(Rhs+1,"d",&m3,&n3,&l3); /* Note that this code is deprecated and should be replaced by API_Scilab */ // CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&m3,&n3,&l3);// In Scilab 5 *stk(l3) = c; LhsVar(1) = Rhs+1; return 0; } /**********************************************************/
- builder.sce :
// This is the builder.sce // must be run from this directory ilib_name = 'libfsum' // interface library name // objects files files = ['sci_fsum.o','fsum.o' ]; libs = [] // other libs needed for linking table =['fortransum','sci_fsum']; // do not modify below // ---------------------------------------------- ilib_build(ilib_name,table,files,libs)
- Execute in Scilab :
___________________________________________ scilab-4.1 Copyright (c) 1989-2006 Consortium Scilab (INRIA, ENPC) ___________________________________________ Startup execution: loading initial environment -->exec builder.sce; ilib_name = libfsum libs = [] generate a gateway file generate a loader file generate a Makefile: Makelib running the makefile compilation of sci_fsum compilation of fsum building shared library (be patient) -->exec loader.sce; shared archive loaded -->fortransum(3,8) ans = 11. -->
That's all.