matlab mex文件和C ++ dll(windows) [英] matlab mex files and C++ dll (windows)

查看:494
本文介绍了matlab mex文件和C ++ dll(windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类Test的DLL。
标题:

I have a DLL with class Test. Header:


class MY_EXPORT Test
{
public:
    int doit(const string &str);
};

和来源:


int 
Test::doit(const string &str)
{
    return int(str.length());
}

现在我使用它从mex文件:

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);
}

问题,变量 str 未正确传递到方法 doit
在方法中它包含rabish。我发现这发生在通过引用传递的任何对象。我做错了什么?请帮助。

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:如果我将声明更改为int doit(const char *),一切运行良好。

PS: if I change declaration to 'int doit(const char *)' everything working well.

推荐答案

问题是这样:

libmex.dll(和整个Matlab 2010a / 2010b) Microsoft.VC80.CRT(version = 8.0.50727.4053)

但是您的Visual Studio使用Microsoft.VC90.CRT(版本= 9.0.21022.8)

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)

如果你写一个C ++ mex文件,你需要使用相同版本的CRT lib在你的mex dll什么matlab使用。
您可以免费安装Visual C ++ 2005(SP1)Express Edition,并用该文件编译mex文件。

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.

这篇关于matlab mex文件和C ++ dll(windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆