2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2009 - DIGITEO - Allan CORNET
4 // Copyright (C) 2018 - ESI Group - Clement DAVID
6 // This file is distributed under the same license as the Scilab package.
9 // CALLING EXTERNAL C FUNCTION
11 if haveacompiler() then
14 // we use TMPDIR for compilation
16 f1=["#include <math.h>";
17 "void bar(double *a,double *b,double *c)";
18 "{ *c = expm1(*a * log1p(*b)); }" ];
20 i=["#include <stdlib.h>"
21 "#include <api_scilab.h>"
22 "#include <Scierror.h>"
23 "#include <localization.h>"
25 "extern int bar(double *x, double *y, double *z);"
27 "int sci_bar(char *fname, void* pvApiCtx)"
31 " int m1 = 0, n1 = 0;"
32 " double *pdVarOne = NULL;"
33 " int *piAddressVarOne = NULL;"
34 " int m2 = 0, n2 = 0;"
35 " double *pdVarTwo = NULL;"
36 " int *piAddressVarTwo = NULL;"
37 " double *pdblOut = NULL;"
39 " CheckInputArgument(pvApiCtx, 2, 2);"
40 " CheckOutputArgument(pvApiCtx, 0, 1);"
42 " sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);"
45 " printError(&sciErr, 0);"
49 " sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &pdVarOne);"
52 " printError(&sciErr, 0);"
56 " sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);"
59 " printError(&sciErr, 0);"
63 " sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &pdVarTwo);"
66 " printError(&sciErr, 0);"
70 " sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 1, &pdblOut);"
73 " printError(&sciErr, 0);"
77 " bar(pdVarOne, pdVarTwo, pdblOut);"
79 " AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;"
80 " ReturnArguments(pvApiCtx);"
85 mprintf(gettext("Calling a C function from Scilab.\n"));
89 if ~c_link("bar") then
90 cur_ilib_verbose = ilib_verbose();
98 mprintf(gettext("Calling ilib_for_link to build C function.\n"));
99 lib_ = ilib_for_link(["bar"], "bar.c", [],"c");
100 link(lib_, "bar", "c");
101 ilib_build("gw_bar",["bar" "sci_bar"],"sci_bar.c",basename(lib_));
104 ilib_verbose(cur_ilib_verbose);
107 //Z = X+Y by C function
111 mprintf(gettext("Calling C function. Z = (1+Y)**X - 1"));
113 mprintf(gettext("with X = %d"), X);
115 mprintf(gettext("with Y = %d"), Y);
117 mprintf("Z = bar(X, Y);");
120 mprintf(gettext("Result Z = %d"), Z);