VS与matlab混编之VS调用matlab库文件
很长一段时间以来,很多算法工程师会基于matlab进行算法原型的开发,然后移植到C++等语言进行实时验证。但移植的过程,本身是浪费时间的,需要两个语言的转换,甚至不同的工程师来理解算法原型。基于此,现总结出一套VS与matlab混编的流程,以快速验证算法原型或做产品demo。
1.安装matlab:本文使用matlab r2018a,其支持的VS工具包括VS2013~2017;其他版本支持情况见’https://ww2.mathworks.cn/support/requirements/previous-releases.html’
2.安装VS:本文使用VS2013(可离线更新许可证),也可在联网电脑使用VS15/17。
3.为matlab安装C/C++编译器:18a支持的编译器版本为X86_64-5.3.0-release-win32;
安装:使用附件’mingw-w64-install.exe’进行在线安装,格式见下
安装完成后,在rootpath/bin目录下,使用cmd输入gcc -v,得到以下:
4.为mingw添加环境变量:有以下两种方式
a.系统变量中添加变量名和rootpath
b.matlab命令行键入变量名和rootpath(重启matlab后生效)
5.计算机环境的编译器设置:有以下两种方式(可通过slrtgetCC(‘supported’)查看支持的编译器)
a.通过slrsetCC(‘setup’)命令,按照顺序完成编译器设置
b.通过slrsetCC(‘VisualC’,’rootpath’)命令设置编译器
6.Matlab创建需要编译的m文件:例程addmatrix.m
function a=addmatrix(a1,a2)
a=a1+a2;
7.Matlab库文件编译:通过App->Library Compiler->C++ Shared Library,并添加addmatrix.m文件作为输入,则默认得到addmatrix.h,addmatrix.dll,addmatrix.lib等三个主要的输出。
8.创建VS C++工程:首先
a.环境搭建:
1)包含目录添加’matlab rootpath\extern\include’
2)库目录添加’matlab rootpath\extern\lib\win64\microsoft’
3)附件依赖项添加libeng.lib;libmat.lib;libmex.lib;libmx.lib;mclmcrrt.lib;mclmcr.lib;addmatrix.lib
9.源文件例程demo1.cpp
#include "stdafx.h"
#include "libmatrix.h"
#include <iostream>
int run_main(int argc, const char **argv)
{
if (!libmatrixInitialize())
{
std::cerr << "Could not initialize the library properly"
<< std::endl;
return -1;
}
else
{
try
{
// Create input data
double data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
mwArray in1(3, 3, mxDOUBLE_CLASS, mxREAL);
mwArray in2(3, 3, mxDOUBLE_CLASS, mxREAL);
in1.SetData(data, 9);
in2.SetData(data, 9);
// Create output array
mwArray out;
// Call the library function
addmatrix(1, out, in1, in2);
// Display the return value of the library function
std::cout << "The sum of the matrix with itself is:" << std::endl;
std::cout << out << std::endl;
}
catch (const mwException& e)
{
std::cerr << e.what() << std::endl;
return -2;
}
catch (...)
{
std::cerr << "Unexpected error thrown" << std::endl;
return -3;
}
// Call the application and library termination routine
libmatrixTerminate();
}
// mclTerminateApplication shuts down the MATLAB Runtime.
// You cannot restart it by calling mclInitializeApplication.
// Call mclTerminateApplication once and only once in your application.
mclTerminateApplication();
return 0;
}
// The main routine. On the Mac, the main thread runs the system code, and
// user code must be processed by a secondary thread. On other platforms,
// the main thread runs both the system code and the user code.
int main(int argc, const char **argv)
{
// Call application and library initialization. Perform this
// initialization before calling any API functions or
// Compiler SDK-generated libraries.
if (!mclInitializeApplication(nullptr, 0))
{
std::cerr << "Could not initialize the application properly"
<< std::endl;
return -1;
}
return mclRunMain(static_cast<mclMainFcnType>(run_main), argc, argv);
}
编译工程:首先将addmatrix.h,addmatrix.dll,addmatrix.lib三个文件拷贝到C++工程目录,有以下两种方式编译得到demo1.exe
a.进入C++工程目录,matlab命令行输入:mbuild demo1.cpp libmatrix.lib
b.cmd命令行进入C++工程目录,输入:mbuild demo1.cpp libmatrix.lib
执行demo1.exe得到以下结果