Mex & shared library

5 次查看(过去 30 天)
jason beckell
jason beckell 2012-1-23
Hello to everyone,
I'm a student and I'm not very expert in Matlab. I am writing a mex file (double.c) under Linux which should call a routine from a shared library (lidouble.so). How can I do that? Which command should I write to fulfill my purpose?
Thanks to everyone for your kind attention and my best regards,
Jason.

回答(4 个)

Titus Edelhofer
Titus Edelhofer 2012-1-23
Hi,
usually it is sufficient to do the following: add an include statement for the library to your mex file, so that the compiler "knows" the functions. Second, when compiling your code with mex add the library using the switch "-l" (see doc).
Titus

Kaustubha Govind
Kaustubha Govind 2012-1-23
This is what you are doing:
y = mxGetPr(plhs[0]);
y = timestwo(x);
The function timestwo overwrites the input pointer 'x' with two times its value and returns it - effectively overwriting the pointer 'y' with the location of 'x'. Therefore the original memory that was allocated for the output remains untouched:
plhs[0] = mxCreateDoubleMatrix(mrows, ncols, mxREAL);
Try making it:
*y = *(timestwo(x));
So that the values are copied instead of the pointer getting overwritten. Does this fix the issue?

jason beckell
jason beckell 2012-1-23
Hello Kaustubha,
thank you very much for your precious help, now I got it clear and your solution definitely works! :)
Thank you very much again!
Jason.
  1 个评论
Kaustubha Govind
Kaustubha Govind 2012-1-23
Glad to be of help. Also, please note that my solution works only as long as the output is a scalar. In case of an array/matrix, you'll have to loop over the value returned by timestwo and copy it into y.

请先登录,再进行评论。


jason beckell
jason beckell 2012-1-23
Hello Titus,
thank you very much, now it finally works, thank you again! Anyway, now I have a further little question to ask you. Just to produce an example, I wrote down the following mex file (test.c)
#include "mex.h" #include "double.h" #include "matrix.h"
void myExitFcn() { mexPrintf("MEX-file is being unloaded"); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *x, *y; int mrows, ncols;
/* Check for proper number of arguments. Just one input and one output*/
if (nrhs != 1) {
mexErrMsgTxt("One input required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments");
}
/* The input must be a noncomplex scalar double.*/
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
!(mrows == 1 && ncols == 1)) {
mexErrMsgTxt("Input must be a noncomplex scalar double.");
}
/* Assign pointers to each input and output. */
x = mxGetPr(prhs[0]);
plhs[0] = mxCreateDoubleMatrix(mrows, ncols, mxREAL);
y = mxGetPr(plhs[0]);
/*Print the value of x */
mexPrintf("Value x: %f \n\n", *x);
/*call the routine timestwo which is listed in double.h */
y=timestwo(x);
/*Print the value of y */
mexPrintf("Value y: %f \n\n", *y);
if(mexAtExit(myExitFcn))
{
mexPrintf("Error unloading function!");
}
}
Let this be the file double.h
double* timestwo(double* x);
and let this be file double.c
#include math.h #include stdio.h
double* timestwo(double* x) { (x) = 2.0 (*x); return x; }
Let a=8, if I compile it and then I type:
b = test(a)
I get the following outputs
Value x: 3.000000
Value y: 6.000000
c =
0
How come c = 0? Shouldn't it be equals to 6.000000? Thank you!
  1 个评论
Walter Roberson
Walter Roberson 2012-1-23
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by