Calling C functions using MATLAB.
이전 댓글 표시
I have a simple c function stored in clibs.c. The C file looks like the following:
/////clibs.c
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <mex.h>
#include "shrhelp.h"
#include "clibs.h"
EXPORTED_FUNCTION void twoBP(double *xdot, double *x){
xdot[0] = x[3];
xdot[1] = x[4];
xdot[2] = x[5];
xdot[3] = x[0];
xdot[4] = x[1];
xdot[5] = x[2];
return;
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray*prhs[] )
{
}
////end clibs.c
I also have the associated header file (clibs.h):
////clibs.h
#ifndef clibs_h
#define clibs_h
#include "shrhelp.h"
/* Function declarations */
EXPORTED_FUNCTION void twoBP(double *, double *);
#endif
///end clibs.h
In order to call twoBP function form MATLAB I simply used the follwing statemes:
mex clibs.c
loadlibrary('clibs')
xdot = zeros(1,6);
x = [1,2,3,4,5,6];
xdot = calllib('clibs','twoBP',xdot,x);
The enviroment is working and it returns xdot = [4,5,6,1,2,3] as expected.
Furthermore I can verify that the library had been well loaded by typing in the command window...
>>libfunctions('clibs')
which returns...
Functions in library clibs:
twoBP
My problem appears when I try to compile a slightly different C function:
EXPORTED_FUNCTION void twoBP(double *xdot, double *x,void (*camp)(double x, double *y, int n, double *f)){
xdot[0] = x[3];
xdot[1] = x[4];
xdot[2] = x[5];
xdot[3] = x[0];
xdot[4] = x[1];
xdot[5] = x[2];
return;
}
As you can see this new twoBP function takes an extra input argument: a function defined as void (*camp)(double x, double *y, int n, double *f).
I modify clibs.h accordingly:
#ifndef clibs_h
#define clibs_h
#include "shrhelp.h"
/* Function declarations */
EXPORTED_FUNCTION void twoBP(double *, double *,void (*)(double, double *, int, double *));
#endif
However, when I try to execute the matlab code...
mex clibs.c
loadlibrary('clibs')
libfunctions('clibs')
It returns:
No methods for class lib.clibs.
I do not know how to fix this problem. Any idea?
Thank you for your attention.
Estel.
답변 (2개)
James Tursa
2020년 1월 29일
편집: James Tursa
2020년 1월 29일
This:
void (*camp)(double x, double *y, int n, double *f)
is not a function as you claim. It is a pointer to a function that has the specified signature (takes four inputs and doesn't return anything). So this expression that you have in the prototype
void (double, double *, int, double *)
is incorrect. I am guessing this is what you fed to MATLAB. Had you fed it into a compiler I think you would have gotten an error. The proper expression is the first one. Simply use that in your h file. Or if you don't like the variable names (they are optional and don't hurt anything), then just
void (*)(double, double *, int, double *)
Your prototype in the h file would be (if I remove that extra double which doesn't appear in the actual function signature):
EXPORTED_FUNCTION void twoBP(double *, double *,void (*)(double, double *, int, double *));
Estel Ferrer Torres
2020년 1월 30일
편집: Estel Ferrer Torres
2020년 1월 30일
댓글 수: 4
James Tursa
2020년 1월 30일
편집: James Tursa
2020년 1월 30일
I don't use loadlibrary myself, so I am not familiar with what function signatures might cause it problems. That being said, I would wonder what you think MATLAB should do with that function pointer. MATLAB calllib certainly can't pass it anything proper for that argument, so what would you expect it to do? Can you clarify how you intend to call your twoBP function from MATLAB? And in particular what you intended for that function pointer argument? Even for your simple case where loadlibrary detects the twoBP function, there is no way it can pass anything valid for that function pointer argument. So what do you expect to happen when you call it?
Estel Ferrer Torres
2020년 1월 30일
James Tursa
2020년 1월 31일
I don't know how to advise based on what I know so far. How can Python pass a C function pointer to a C library?
Estel Ferrer Torres
2020년 1월 31일
편집: Estel Ferrer Torres
2020년 1월 31일
카테고리
도움말 센터 및 File 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!