3

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

MY_EXPORT 类测试
{
上市:
    int doit(const string &str);
};

和来源:

整数
测试::doit(const string &str)
{
    返回 int(str.length());
}

现在我从 mex 文件中使用它:

空白
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    字符串 str("你好!");
    测试 *t = new Test();
    t->doit(str);
}

问题是,该变量str未正确传递给方法doit。在方法内部,它包含 rabish。我发现通过引用传递的任何对象都会发生这种情况。我做错了什么?请帮忙。

PS:如果我将声明更改为 'int doit(const char *)' 一切正常。

4

1 回答 1

5

问题是这样的:
libmex.dll(和整个 Matlab 2010a/2010b)使用 Microsoft.VC80.CRT(版本=8.0.50727.4053)
但是您的 Visual Studio 使用 Microsoft.VC90.CRT(版本=9.0.21022.8)

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

于 2011-03-28T17:57:17.800 回答