mathwork官网解决方案,说的很详细啦。
https://cn.mathworks.com/matlabcentral/answers/94471-how-do-i-create-a-c-c-shared-library-with-matlab-compiler-that-can-be-used-in-a-microsoft-visual-c
直接上我的代码吧:
matlab的m文件,两个输入变量,没有输出
function cameraCalibrator(basis,numImages) ... ... ... end
然后在命令行中运行
mcc -W cpplib:camCalibMatlab -T link:lib cameraCalibrator.m
将生成的dll,lib和h文件copy到c++代码路径下。
c++中的调用为:
bool CamCalibrator::run() { if (!camCalibMatlabInitialize()) { return false; } cout<<endl; cout<<"Start matlab code!"<<endl; cout<<endl; mwArray _basis(1,1,mxDOUBLE_CLASS); mwArray _numImage(1,1,mxDOUBLE_CLASS); _basis.SetData(&basis,1); _numImage.SetData(&numImage,1); cameraCalibrator(_basis,_numImage); // terminate MCR mclTerminateApplication(); cout<<"Matlab create XML file!"<<endl; return true; }
1)camCalibMatlabInitialize是必须的;
2)mwArray 借口类型;
3)mclTerminateApplication 关闭调用。
代码下载: https://github.com/zhanxiage1994/Cplusplus-matlabCalibrationCamera.git