difficulties making wrapper mex file on linux?

Hi,
First I complied the fcnsuit.c file as follows in a linux terminal:
gcc -c -fPIE fcnsuite.c ld -o fcnsuite.so -shared fcnsuite.o
Then I open matlab and try to make a mex file as follows:
mex mlbsuite.c
Then I get following error message:
mlbsuite.o:In function 'mexFunction':
mlbsuite.c:(.text+0xae): undefined reference to 'dlopen'
mlbsuite.c:(.text+0xd6): undefined reference to 'dlsym'
mlbsuite.c:(.text+0x217): undefined reference to 'dlclose'
mex: link of ' "mlbsuit.mex64"' failed
How can I fix this?

 Réponse acceptée

Kaustubha Govind
Kaustubha Govind le 6 Août 2012
Modifié(e) : Kaustubha Govind le 6 Août 2012
Looks like you might need to add a
#include <dlfcn.h>
to mlbsuite.c?

7 commentaires

This is the code in mlbsuite.c:
/* OS specific function calls for shared objects and dynamic linking */ { #ifdef WINDOWS
#include windows.h
#include process.h
typedef void (WINAPI * PPROC) (double *, double *, double *, double *, int, int, int, int);
#define LIBHANDLE HANDLE
#define GetProcedure GetProcAddress
#define CloseDynalink FreeLibrary
#else
#include dlfcn.h
#include pthread.h
typedef void (*PPROC) (double *, double *, double *, double *, int, int, int, int);
#define LIBHANDLE void *
#define GetProcedure dlsym
#define CloseDynalink dlclose
#endif
#include "mex.h"
#ifdef _STDC_
void mexFunction (int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
#else
void mexFunction (nlhs, plhs, nrhs, prhs)
int nlhs, nrhs;
mxArray *plhs[];
const mxArray *prhs[];
#endif
int i, j, m, n, ng, nh, size;
double *H, *G, *F, *X, *tmpptr;
char *fcnname;
PPROC pfcn;
LIBHANDLE hLibrary;
if ((nrhs < 4) || (nlhs < 3))
{
mexPrintf ("usage: [f, g, h] = mlbsuite(x, ng, nh, 'function_name');\n");
mexErrMsgTxt ("example: [f, g, h] = mlbsuite([14.095 0.84296]', 2, 0, 'g06');");
}
n = mxGetM (prhs[0]);
m = mxGetN (prhs[0]);
X = mxGetPr (prhs[0]);
size = mxGetNumberOfElements (prhs[3]) + 1;
fcnname = mxCalloc (size, sizeof (char));
if (mxGetString (prhs[3], fcnname, size) != 0)
mexErrMsgTxt ("Could not convert string data.");
#ifdef WINDOWS
hLibrary = LoadLibrary ("fcnsuite.dll");
#else
hLibrary = dlopen ("./fcnsuite.so", RTLD_NOW);
#endif
if (NULL == hLibrary)
mexErrMsgTxt ("could not load fcnsuite.dll (win32) or fcnsuite.so (linux) file");
pfcn = (PPROC) GetProcedure (hLibrary, fcnname);
if (NULL == pfcn)
{
mexPrintf ("procedure %s not found in library file!", fcnname);
mexErrMsgTxt ("failed to load procedure");
}
plhs[0] = mxCreateDoubleMatrix (1, m, mxREAL);
F = mxGetPr (plhs[0]);
tmpptr = mxGetPr (prhs[1]);
ng = (int) tmpptr[0];
plhs[1] = mxCreateDoubleMatrix (ng, m, mxREAL);
G = mxGetPr (plhs[1]);
tmpptr = mxGetPr (prhs[2]);
nh = (int) tmpptr[0];
plhs[2] = mxCreateDoubleMatrix (nh, m, mxREAL);
H = mxGetPr (plhs[2]);
for (i = 0; i < m; i++)
{
pfcn (&X[i * n], &F[i], &G[i * ng], &H[i * nh], n, 1, ng, nh);
}
CloseDynalink (hLibrary);
mxFree (fcnname);
}
As you can see
#include dlfcn.h
was already in it.
Thank you for your help.
Kaustubha Govind
Kaustubha Govind le 7 Août 2012
Ah! Is it just the linking (and not the compiling) that fails? If yes, you may need to link against an additional library using the mex -l option.
Jarir
Jarir le 7 Août 2012
Thank you!
Problem is I am just a newbie in all this. I actually don't know how to do what you are suggesting. Which library do I have to link and how?
Try:
>> mex mlbsuite.c -ldl
MTA
MTA le 8 Août 2012
Hi I'm having the same problem but when running my simulink model in the accelerator mode (which will compile the model and hence the same problem) The option -ldl is already shows in the compilation commands being executed, I've also provided a link to the libdl.so library so Matlab can see it and still no luck
Cheers
Mohamad
Kaustubha Govind
Kaustubha Govind le 8 Août 2012
Mohamad: Could you please start a new thread for this if you don't mind? Since you are working with Simulink, there might be other considerations. Also, I would recommend that you post the complete build log in your question. Thanks!
Jarir
Jarir le 8 Août 2012
Kaustubha Govind: Thank you very much! It finally works.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by