2012-12-11 170 views
5

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

现在我想与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 
+0

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

回答

0

我发现它使如果使用MCR或已安装MATLAB安装,则会有很大差异。

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