【问题标题】:Create a DLL from MATLAB从 MATLAB 创建 DLL
【发布时间】:2012-11-28 19:39:17
【问题描述】:

我在 MATLAB 中创建了一个 DLL,它为我的 .m 函数提供了一个接口。

现在我想将它与 MCR 运行时库一起使用。 (MCR = Matlab 编译器运行时)。

我从一个 C 例程中调用这个 DLL,该例程最终用 GCC (MinGW) 编译成一个包装 DLL。

现在我的函数变成了两种形式:

extern LIB_XYZ_C_API 
bool MW_CALL_CONV mlxGet_path(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);
extern LIB_XYZ_C_API bool MW_CALL_CONV mlfGet_path(int nargout, mxArray** p);

从这些中我选择了后者,因为前者似乎是一种“老式/传统”。

我这样称呼它:

char get_path(LStrHandle path)
{
    char mret = init_XYZ(); // here I call mclmcrInitialize(), mclInitializeApplication(NULL, 0) etc.
    if (mret) return mret;
    mret = 2;
    // here the relevant part begins
    mxArray * mxpath = NULL; // set it to NULL and let the callee allocate it
    bool bret = mlfGet_path(1, &mxpath);
    // now I convert the mxpath to a string
    // What do I do with the mxpath afterwards?
    // I try to free it with
    mxDestroyArray(mxpath);
    return mret;
}

问题就从这里开始:在链接过程中找不到mxDestroyArray()

undefined reference to `mxDestroyArray'

如果我手动将-llibmx 添加到构建过程中,构建会运行,但随后找不到libmx.dll,因为MCR 仅将$MCR\runtime\win32 放入路径中,而不是$MCR\bin\win32,其中libmx.dll生活。

我能做什么?

当我使用自编译DLL时,是否必须选择不同的“销毁”函数?

或者我必须在路径上胡闹? (我不希望如此......)

此外,还有其他一些功能缺失,但我认为这会以同样的方式解决:

mxGetNumberOfElements
mxIsDouble
mxGetPr
mxGetM
mxGetN
mxGetData
mxIsChar
mxIsCell
mxDestroyArray
mxGetCell_730
mxSetCell_730
mxGetString_730
mxCalcSingleSubscript_730
mxGetNumberOfDimensions_730
mxCreateDoubleMatrix_730
mxCreateNumericMatrix_730
mxCreateCellMatrix_730

【问题讨论】:

  • 不确定这是否是解决方案,但最新版本的 Matlab 允许您将未自动链接的文件(编译前)添加到项目中。

标签: matlab linker matlab-deployment matlab-compiler


【解决方案1】:

我发现使用 MCR 或安装 MATLAB 安装会有很大的不同。

  1. 使用-lmclmcrrt 而不是-lmx,并为链接器使用正确的库路径。
  2. 在编译中使用的每个文件中使用正确的#include 文件。特别是,不要将#include "matrix.h" 和与 MATLAB DLL 一起创建的头文件混用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多