[scilab-Users] Link Scilab/C : Problem passing char* to a C function from Scilab

Antoine ELIAS antoine.elias at scilab.org
Wed May 5 17:02:01 CEST 2010


hello Charles,

I made some tests and it seems you have to add "out" in the call of "call".

call('callQuantitative',model,1,'c',lengthModel,2,'i',nbL,3,'i',nbC,4,'i', 'out')


I wrote a short example inspired by your function :

//function declaration
functionDeclaration=['#include<stdio.h>'
'void callQuantitative(char* model, int* lengthModel, int* nbL, int* nbC)'
'{'
'  printf(""model = \""%s\""\n"", model);'
'  printf(""lengthModel = %d\n"", *lengthModel);'
'  printf(""nbL = %d\n"", *nbL);'
'  printf(""nbC = %d\n"", *nbC);'
'}'];

cd TMPDIR;
mputl(functionDeclaration, 'callQuantitative.c')

//creating the shared library (a gateway, a Makefile and a loader are generated.
ilib_for_link('callQuantitative', 'callQuantitative.c', [], "c")
exec loader.sce

model = ["Test"];
lengthModel = length(model);
[nbL, nbC] = size(model);
// Finally we call the function
call('callQuantitative', model, 1, 'c', lengthModel, 2, 'i', nbL, 3, 'i', nbC, 4, 'i', 'out');

output in terminal :

model = "Test"
lengthModel = 4
nbL = 1
nbC = 1

Regards,

Antoine

Le 05/05/2010 15:23, Charles G. a écrit :
> Hello,
>
> I'm currently working on Scilab functions and I need to call a C++ function.
> To do so, I need to go through a C function which does the link between
> C++ and Scilab.
>
> I succeeded in calling dynamically the C function from Scilab, but I still
> have an issue passing matrix of char* as an "in" argument.
>
> With the following function :
>
> void callQuantitative(char* model, int* lengthModel, int* nbL, int* nbC){
>      printf("%s\n",model);
> }
>
> I tried the following example :
>
> /* These 3 first lines are to load dynamically the C function */
> exec('callQuantitative.sce');
> exec('functionLoader.sce');
> loadFunction('callQuantitative','libcallQuantitative');
> /* Then we initialize the arguments we are going to give to the C function */
> model = ["Test"];
> lengthModel = length(model);
> [nbL,nbC] = size(model);
> /* Finally we call the function */
> call('callQuantitative',model,1,'c',lengthModel,2,'i',nbL,3,'i',nbC,4,'i');
>
> As a result, I get the following error message :
>
> call('callQuantitative',model,1,'c',lengthModel,2,'i',nbL,3,'i',nbC,4,'i')
> ans  =
>
> i
>
> Attention: problème avec la pile ... nettoyé.
>
> The error message is : "Warning: Problem with the stack ... cleaned)."
>
> Note that the printf in the C function is done but weirdly in writes
> the name of the C function (ie. "callQuantitative").
>
>
> Also another issue I encountered concerns 2D float or integer matrix.
>
> It seems that if we give a 2D matrix to a C function, it is actually
> given as a single dimension matrix.
>
> So I would like to know if it's a normal behavior or if it's possible to
> actually give the 2D matrix as to be used as a real 2D matrix in the
> C function.
>
> Thank you.
>
> Charles G.
>
>    


-- 
Antoine ELIAS
Software developer
-------------------------
The Scilab Consortium
Digiteo
Domaine de Voluceau
Rocquencourt - B.P. 105
78153 Le Chesnay France
Phone: +33.1.39.63.50.63




More information about the users mailing list