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

 채택된 답변

Walter Roberson
Walter Roberson 2015년 9월 28일

0 개 추천

In order to do that you would have to loadlibrary() the .mex* . That is possible as .mex* files are actually dll. You would need an appropriate .h file . You would need to be careful about which functions expect to be called in the standard MATLAB to mex interface, and which functions expect to be called using C interface. You would also need to be careful about who owns which memory.

댓글 수: 4

KSSV
KSSV 2015년 9월 28일
Hi Walter Roberson
Thanks a lot once again for your quick reply.....can you be more brief about the gist. My mexfile is .mexw64. Can you suggest me a reference or an example problem on this?
Regards Sreenivas
James Tursa
James Tursa 2015년 9월 28일
편집: James Tursa 2015년 9월 28일
I would have said that in general, no you cannot do this because the functions in question are likely not exported, so loadlibrary will not know where they physically are in the dll. Just because you have a header file does not mean that loadlibrary will have the address for the start of a function in the library. For that it would need to be exported. In a typical mex build the only function that is exported is mexFunction. If you have the source code you could alter other function definitions so they get exported as well. Then after a recompile you might be able to call these other functions via loadlibrary depending on what they do.
Walter Roberson
Walter Roberson 2015년 9월 28일
All functions in C are automatically extern unless they are marked static, so this is just a matter of relinking the object code. For Microsoft compilers that is done with the /export linker option; https://msdn.microsoft.com/en-us/library/7k30y2k5.aspx
James Tursa
James Tursa 2015년 9월 29일
편집: James Tursa 2015년 9월 29일
The mex command automatically deletes the object files associated with the compiling, so these typically are not available after the fact. Hence a recompile will probably be necessary with appropriate link options to export the functions.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

James Tursa
James Tursa 2015년 9월 29일
편집: James Tursa 2015년 9월 29일

0 개 추천

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.

댓글 수: 1

KSSV
KSSV 2015년 9월 30일
Hi James Tursa
Thanks a lot for detailed explanation and crispy code.
Regards
Sreenivas

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품

질문:

2015년 9월 28일

댓글:

2015년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by