我在 MATLAB 中创建了一个 DLL,它为我的 .m 函数提供了一个接口(interface)。
现在我想将它与 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
最佳答案
我发现,如果使用 MCR 或安装了 MATLAB 安装,这会有很大的不同。
- 使用
-lmclmcrrt
而不是-lmx
并为链接器使用正确的库路径。 - 在编译中使用的每个文件中使用正确的
#include
文件。特别是,不要混合使用#include "matrix.h"
和与 MATLAB DLL 一起创建的头文件。
关于matlab - 从 MATLAB 创建 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13817088/