今天帮人调试国外牛人ZK所编写的MATLAB 与C++混合编译程序,耗时一个晚上,经过各种百度,各种谷歌,终于解决所出现的错误。不得不说GitHub的牛人回复真是给力!这里简要记录一下步骤。
1. MATLAB 编译器配置。
命令串口 mex -setup ,出现如下提示:
MEX 配置为使用 'Microsoft Visual C++ 2010 (C)' 以进行 C 语言编译。 Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. In the near future you will be required to update your code to utilize the new API. You can find more information about this at: http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
要选择不同的语言,请从以下选项中选择一种命令: mex -setup C++ mex -setup FORTRAN
1.1 输入mex -setup C++ 更改为C++编译器
MEX 配置为使用 'Microsoft Visual C++ 2010' 以进行 C++ 语言编译。
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
2. OpenCV 设置
2.1 值得注意的一点是:opencv的路径不要有空格,我一开始的解压路径在d:\program files\下,就怎么运行都不对
3. 更改compile.m下的路径
将原本的: include = ' -Ic:\OpenCV2.2\include\opencv\ -Ic:\OpenCV2.2\include\';
libpath = 'c:\OpenCV2.2\lib\';
更改为本机所安装OpenCV的目录即可,例如:
include = ' -ID:\opencv\build\include\opencv\ -ID:\opencv\build\include\ -ID:\opencv\build\include\opencv2\';
libpath = 'D:\opencv\build\x64\vc10\lib\';
3.1 上面一个需要注意的问题是 如果是64位的MATLAB 程序,请务必选择 x64目录下的vc10\lib,否则将出现如下的错误:
Creating library C:\USERS\WACZKOR\APPDATA\LOCAL\TEMP\MEX_2N~1\templib.x and object C:\USERS\WACZKOR\APPDATA\LOCAL\TEMP\MEX_2N~1\templib.exp
lk.obj : error LNK2019: unresolved external symbol cvReleaseImage referenced in function "void __cdecl normCrossCorrelation(struct _IplImage *,struct _IplImage *,struct CvPoint2D32f *,struct CvPoint2D32f *,int,char *,float *,int,int)" (?normCrossCorrelation@@YAXPEAU_IplImage@@0PEAUCvPoint2D32f@@1HPEADPEAMHH@Z)
lk.obj : error LNK2019: unresolved external symbol cvMatchTemplate referenced in function "void __cdecl normCrossCorrelation(struct _IplImage *,struct _IplImage *,struct CvPoint2D32f *,struct CvPoint2D32f *,int,char *,float *,int,int)" (?normCrossCorrelation@@YAXPEAU_IplImage@@0PEAUCvPoint2D32f@@1HPEADPEAMHH@Z)
lk.obj : error LNK2019: unresolved external symbol cvGetRectSubPix referenced in function "void __cdecl normCrossCorrelation(struct _IplImage *,struct _IplImage *,struct CvPoint2D32f *,struct CvPoint2D32f *,int,char *,float *,int,int)" (?normCrossCorrelation@@YAXPEAU_IplImage@@0PEAUCvPoint2D32f@@1HPEADPEAMHH@Z)
lk.obj : error LNK2019: unresolved external symbol cvCreateImage referenced in function "void __cdecl normCrossCorrelation(struct _IplImage *,struct _IplImage *,struct CvPoint2D32f *,struct CvPoint2D32f *,int,char *,float *,int,int)" (?normCrossCorrelation@@YAXPEAU_IplImage@@0PEAUCvPoint2D32f@@1HPEADPEAMHH@Z)
lk.obj : error LNK2019: unresolved external symbol cvCalcOpticalFlowPyrLK referenced in function mexFunction
lk.obj : error LNK2019: unresolved external symbol cvAlloc referenced in function mexFunction
lk.mexw64 : fatal error LNK1120: 6 unresolved externals
4. 编译错误 C4430的解决
在运行compile.m时会出现C4430的错误和若干警告,具体为:d:\program files\matlab 2014a\extern\include\matrix.h(266) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 如下图所示:
解决办法是: 在d:\program files\matlab 2014a\extern\include\matrix.h,即本机的MATLAB安装目录下extern\include\matrix.h中打开该头文件,然后在第一行中添加如下命令: #pragma warning (disable:4430) ,即可解决。(虽然不建议更改软件自带头文件,但在此处暂未找到更加好的方法)
各位可参见github关于该问题的原答案:https://github.com/zk00006/OpenTLD/issues/12 ,另外在此说明一点,经过尝试不可在matrix.h所添加的语句后添加任何解释语句,本人本想添加以便标记,没想到后面就一直报错了,只有去除之后才运行成功。这里弄不清楚为何。
5. 至此可成功编译compile.m 。
环境为MATLAB 2014a + VS 2010+ OpenCV 2.3.1
6. 运行run_TLD.m,出现如下错误:
这个的错误在Moondark的博客中已经提及,具体修改方法参见:http://www.cnblogs.com/moondark/archive/2012/04/12/2444602.html,更改lk.cpp中186行‘0’为‘status’,重新compile后再次运行即可。结果截图如下:
By Martin_Ma 2015-5-3