从MATLAB创建一个DLL

glg*_*lgl 5 matlab linker matlab-deployment matlab-compiler

我在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);
Run Code Online (Sandbox Code Playgroud)

从这些我选择后者,因为前者似乎是一种"旧式/遗产".

我这样称呼它:

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;
}
Run Code Online (Sandbox Code Playgroud)

这里麻烦开始了:mxDestroyArray()在链接过程中找不到:

undefined reference to `mxDestroyArray'
Run Code Online (Sandbox Code Playgroud)

如果我手动添加-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
Run Code Online (Sandbox Code Playgroud)

glg*_*lgl 0

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

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