matlab mex files and C++ dll (windows)

假如想象 提交于 2019-12-12 14:42:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!