问题
I have a DLL with class Test. Header:
class MY_EXPORT Test { public: int doit(const string &str); };
and source:
int Test::doit(const string &str) { return int(str.length()); }
Now I use it from mex file:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { string str("hello!"); Test *t = new Test(); t ->doit(str); }
The problem, that variable str
is not passed correctly to the method doit
.
Inside the method it contains rabish. I found that this happens with any object passed by reference. What I am doing wrong? please help.
PS: if I change declaration to 'int doit(const char *)' everything working well.
回答1:
The problem is this:
libmex.dll (and a whole Matlab 2010a/2010b) uses Microsoft.VC80.CRT (version=8.0.50727.4053)
But your Visual Studio uses Microsoft.VC90.CRT (version=9.0.21022.8)
If you write a C++ mex file, you need to use the same version of the CRT lib in your mex dll what the matlab uses. You can install the Visual C++ 2005 (SP1) Express Edition for free, and compile the mex file with that.
来源:https://stackoverflow.com/questions/5460417/matlab-mex-files-and-c-dll-windows