【问题标题】:Mex function unresolved externalMex 函数未解析的外部
【发布时间】:2016-05-10 03:03:29
【问题描述】:

我正在尝试在 MATLAB 中构建一个 mex 函数。该函数依赖于 C++ 库。但是,无论我做什么,我都会在 MATLAB 中得到未解决的外部问题。我创建了三个简单的文件来演示这个问题:

my_test123.h

_declspec(dllexport) void my_test();

my_test.cpp

extern "C" {
#include "my_test123.h"
}
void my_test() {
}

我使用命令编译并链接上面的两个文件:

cl /LD /Femy_test.dll my_test.cpp

这会生成两个文件,my_test.libmy_test.dll

第三个文件是一个微不足道的 mexfunction:

my_mex.cpp

#include "mex.h"

extern "C" {
void my_test(); 
}

/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
    my_test();
}

在 MATLAB 中,我使用以下命令:

mex  -v my_mex.cpp my_test.lib

我也试过了:

mex  -v my_mex.cpp -lmy_test.lib

所有文件都在同一个目录中,并且 mex 命令正在查找 .lib 文件(如果我尝试使用随机名称而不是 my_test.lib ,则会收到文件未找到错误)。

我得到的错误是:

使用 mex 时出错 创建库 my_mex.lib 和对象 my_mex.exp my_mex.obj:错误 LNK2019:函数 mexFunction 中引用的未解析外部符号 my_test my_mex.mexw64 : 致命错误 LNK1120: 1 unresolved externals

我还尝试将每个文件都制作为 C 文件(删除 externs 并将 mexfunciton 扩展名更改为 .c)并在 C 中编译。但我得到了同样的错误。

我正在使用 Visual Studio 2013 和 64 位版本的 MATLAB 2014b。

非常感谢任何帮助。

【问题讨论】:

    标签: c++ matlab mex


    【解决方案1】:

    经过数小时的工作并在 MathWorks 支持热线的帮助下,我发现了以下内容:

    您需要考虑几个因素:

    • 您的 MATLAB 是 32 位还是 64 位?
    • 您的 mexfunction 的扩展名是 .c 还是 .cpp?
    • 您如何使用外部“C”?
    • .dll 是 64 位 .dll 还是 32 位 .dll?

    假设 .dll 是 32 位 .dll 而 MATLAB 是 32 位

    mex 函数的扩展名为 .cpp,.dll 是 C++ .dll 您无需在 my_test.cpp 或 my_mex.cpp 中添加任何 extern "C"

    mex 函数的扩展名为 .c,.dll 是 C++ .dll 您需要在 my_test.cpp 中添加 "extern "C"*。

    mex 函数的扩展名为 .cpp,.dll 是 C .dll 您不需要将 extern "C" 添加到 my_test.cpp 但您需要在 my_mex.cpp 中添加一个。

    mex 函数的扩展名为 .c,.dll 是 C .dll 您无需在 my_test.cpp 或 my_mex.cpp 中添加任何 extern "C"

    看起来根据 mex 函数文件的扩展名,MATLAB 将其编译为 C 或 C++ 文件。知道了这一点,外部用法应该是有意义的。

    以上所有内容仍然有效,但对于 64 位 MATLAB,但您需要 64 位 dll。

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 2010-12-02
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 2013-11-03
      • 2013-03-04
      相关资源
      最近更新 更多