从 C++ 调用我的 MATLAB 函数时,我遇到了几个错误。主要思路是:先编译一个MATLAB函数并生成DLL文件,然后在C++中包含.h和.lib文件。最后,编写.cpp来测试和调用函数。这是我的详细步骤,请告诉我我错在哪里。
(使用 MATLAB 2012b 和 Visual C++ 2008,Windows 7 64 位)
在 MATLAB 中:
mbuild -setup
并将mex -setup
Visual Microsoft Visual C++ 2008 SP1 设置为编译器。MyAdd.m
在文件夹中创建,C:\Users\WangYudong\Documents\MATLAB\MyAdd_M
功能如下:function [c] = MyAdd(a, b) c = a + b;
mcc -W cpplib:libMyAdd -T link:lib MyAdd
编译MyAdd.m
和生成多个文件,包括、libMyAdd.dll
和其他文件。libMyAdd.h
libMyAdd.lib
在 C++ 中
选择VC++ 目录→包含要添加的文件
E:\MATLAB\R2012b\extern\include
。选择VC++ 目录→要添加的 库文件
E:\MATLAB\R2012b\extern\lib\win64\microsoft
和C:\Users\WangYudong\Documents\MATLAB\MyAdd_M
.选择Linker → Input → Additional Dependencies添加新条目:
mclmcr.lib mclmcrrt.lib libmx.lib libmat.lib libMyAdd.lib
在同一个文件夹中创建一个新的
MyAdd_test.cpp
putlibMyAdd.dll
和。添加头文件和资源文件。_libMyAdd.h
libMyAdd.lib
libMyAdd.h
libMyAdd.h
libMyAdd.lib
的代码MyAdd_test.cpp
是这样的:
#include "mclmcr.h"
#include "matrix.h"
#include "mclcppclass.h"
#include "libMyAdd.h"
int main() {
double a = 6;
double b = 9;
double c;
// initialize lib
if( !libMyAddInitialize()) {
std::cout << "Could not initialize libMyAdd!" << std::endl;
return -1;
}
// allocate space
mwArray mwA(1, 1, mxDOUBLE_CLASS);
mwArray mwB(1, 1, mxDOUBLE_CLASS);
mwArray mwC(1, 1, mxDOUBLE_CLASS);
// set data
mwA.SetData(&a, 1);
mwB.SetData(&b, 1);
// use function: MyAdd
MyAdd(1, mwC, mwA, mwB);
// get data
c = mwC.Get(1, 1);
printf("c is %f\n", c);
// terminate the lib
libMyAddTerminate();
// terminate MCR
mclTerminateApplication();
return 0;
}
最后,结果是
Compiling...
MyAdd_test.cpp
Linking...
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclTerminateApplication_proxy referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _libMyAddTerminate referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl MyAdd(int,class mwArray &,class mwArray const &,class mwArray const &)" (__imp_?MyAdd@@YAXHAAVmwArray@@ABV1@1@Z) referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _libMyAddInitialize referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclGetMatrix referenced in function "public: __thiscall mwArray::mwArray(unsigned int,unsigned int,enum mxClassID,enum mxComplexity)" (??0mwArray@@QAE@IIW4mxClassID@@W4mxComplexity@@@Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclcppGetLastError referenced in function "public: static void __cdecl mwException::raise_error(void)" (?raise_error@mwException@@SAXXZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclcppCreateError referenced in function "public: __thiscall mwException::mwException(void)" (??0mwException@@QAE@XZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _ref_count_obj_addref referenced in function "public: __thiscall mwException::mwException(class mwException const &)" (??0mwException@@QAE@ABV0@@Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _ref_count_obj_release referenced in function "public: virtual __thiscall mwException::~mwException(void)" (??1mwException@@UAE@XZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _error_info_get_message referenced in function "public: virtual char const * __thiscall mwException::what(void)const " (?what@mwException@@UBEPBDXZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_getV_int referenced in function "public: class mwArray __cdecl mwArray::GetPromoted(unsigned int,...)" (?GetPromoted@mwArray@@QAA?AV1@IZZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_set_numeric_mxDouble referenced in function "public: void __thiscall mwArray::SetData(double *,unsigned int)" (?SetData@mwArray@@QAEXPANI@Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_get_numeric_mxDouble referenced in function "public: __thiscall mwArray::operator double(void)const " (??BmwArray@@QBENXZ)
C:\Users\WangYudong\Documents\Visual Studio 2008\Projects\MyAdd_C\Debug\MyAdd_C.exe : fatal error LNK1120: 13 unresolved externals
实际上,上面的工作是我从 C++ 调用自定义 MATLAB 函数的测试。我接下来的工作是将 MATLAB 程序转换为 C++,其中包含图像处理函数,如imread
、edge
、strel
等。我尝试过 MATLAB Coder,但它无法转换 MATLAB 函数。所以我试试上面的方法。这是转换这些功能的有效方法还是应该使用 OpenCV 实现它们?