mex file related.....
Show older comments
Hi
I have a c code and it's mexfile. The c code has a lot of sub functions init. My question is, is it possible to call a function of my wish from mexfile, instead of running the whole mexfile?
Thanks in advance.
Srinivas
Accepted Answer
More Answers (1)
James Tursa
on 29 Sep 2015
Edited: James Tursa
on 29 Sep 2015
If the only thing you have is the mex dll routine (i.e., the mex function itself), then you cannot do this in general because the other function names you want to call were not exported. That is, there is no function address information at the front of the file for the loader to know where these functions physically are in the dll file. Unless you takes specific steps otherwise, the only function name that is exported in a mex routine is mexFunction (or MEXFUNCTION for Fortran). None of the other functions can be seen by the loader so they cannot be called. Also, if you are using the mex command from within MATLAB, then all object files associated with compiling are automatically deleted as part of the build, so you typically will not have those laying around to work with either. If you did the mex building from within an IDE and the object files are still there, then you could potentially just do a relink and specify the functions to be exported.
An example is shown below. I have created a simple mex routine that adds two scalar inputs and returns the result:
// Example file for exporting function, mylibrary.c
#include "mex.h"
double add(double x, double y)
{
return x + y;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double x, y;
if( nrhs != 2 || !mxIsNumeric(prhs[0]) || !mxIsNumeric(prhs[1]) ||
mxGetNumberOfElements(prhs[0]) != 1 || mxGetNumberOfElements(prhs[0]) != 1 ) {
mexErrMsgTxt("Need exactly two numeric scalar inputs");
}
if( nlhs > 1 ) {
mexErrMsgTxt("Too many outputs");
}
x = mxGetScalar(prhs[0]);
y = mxGetScalar(prhs[1]);
plhs[0] = mxCreateDoubleScalar(add(x,y));
}
And the associated header file for the function we want to call:
// Header file mylibrary.h
double add(double x, double y);
Compiling the normal way at the MATLAB command line and then trying to call the function 'add':
>> mex mylibrary.c
Building with 'Microsoft Visual C++ 2013 Professional (C)'.
MEX completed successfully.
>> copyfile('mylibrary.mexw64','mylibrary.dll')
>> loadlibrary('mylibrary')
Warning: The function 'add' was not found in the library
> In loadlibrary at 406
>> libfunctions('mylibrary')
No methods for class lib.mylibrary.
>> calllib('mylibrary','add',3,4)
Error using calllib
Method was not found.
>> mylibrary(3,4)
ans =
7
So, even though the mex routine works just fine, the function 'add' is not found by loadlibrary and can't be called.
Now rebuild the mex routine, but this time with the function 'add' added to the EXPORT list:
>> clear all
>> unloadlibrary('mylibrary')
>> mex LINKFLAGS='$LINKFLAGS /EXPORT:add' mylibrary.c
Building with 'Microsoft Visual C++ 2013 Professional (C)'.
MEX completed successfully.
>> copyfile('mylibrary.mexw64','mylibrary.dll')
>> loadlibrary('mylibrary')
>> libfunctions('mylibrary')
Functions in library mylibrary:
add
>> calllib('mylibrary','add',3,4)
ans =
7
>> mylibrary(3,4)
ans =
7
Now it works, and the results match the direct mex function call.
Bottom line is you need to take steps to export the functions that you want to call in order to tell the loader where the functions physically are in the dll, and a typical mex build will not do that for you, and also will not preserve the object files so a simple relink will not be possible. You will probably need to recompile the mex routine to get those object files so they can be relinked with the exported names.
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!