前言

本文主要研究如何将matlab的.m文件,编译成C++代码,然后分别在windows平台和linux平台完成调用,代码组织采用cmake完成。

整体流程

通过Matlab Compiler可以将matlab的代码编译成C/C++、.NET、Java 或 Python的类库,从而实现对于Matlab程序的调用。本文主要研究C++的应用。

从零开始学习C++调用matlab_linux

.m文件编译

我们写一个非常简单的.m文件,命名为mymagic.m

function out = mymagic(in)
out = magic(in);
end

接下来选择app---》Library Compiler

从零开始学习C++调用matlab_python_02

选择C++编译,依顺序从左往右,最后选择Package进行打包。

从零开始学习C++调用matlab_c++_03

打包成功之后,会生成如下文件,我们重点关注下面两个

从零开始学习C++调用matlab_c++_04

for_redistribution_files_only文件夹主要存在四个文件比较重要:

从零开始学习C++调用matlab_matlab_05

其中windows生成的是.dll文件,linux生成的是so文件。这里有一个.ctf文件,需要拷贝到程序运行路径,这里放了大量matlab运行时的信息。

for_testing文件夹会将生成的.h和.cpp都放置出来,方便我们排查问题故障。其他和上面一样。

从零开始学习C++调用matlab_linux_06

至此,我们完成了.m文件的编译,并生成了相应的C++库。

C++调用

这里要注意生成的C++函数的参数和matlab不太一样,我们找到生成的函数的定义:

LIB_MyMatrixFunctions_CPP_API void MW_CALL_CONV 
mymagic(int nargout, mwArray& out, const mwArray& in)
{
mclcppMlfFeval(_mcr_inst, "mymagic", nargout, 1, 1, &out, &in);
}

我们仔细看发现这个函数有3个入参,第一个表示返回值的个数,后面为返回的值,最后一个才是我们的输入的参数。

写一个简单的main函数如下:

MyMatrixFunctionsInitialize函数执行的是初始化工作,加载前面生成的ctf文件,必须在使用之前进行调用。

// CMakeProject.cpp : Defines the entry point for the application.
//


#include "CMakeProject.h"
#include "MyMatrixFunctions.h"


using namespace std;


int main()
{
cout << "Hello CMake." << endl;
if (!MyMatrixFunctionsInitialize())
{
std::cerr << "Could not initialize the library properly"
<< std::endl;
return -1;
}
mwArray mw1;
mwArray mw2(3);
mymagic(1, mw1, mw2);


std::cout << "The sum of the matrix with itself is:" << std::endl;
std::cout << mw1 << std::endl;
return 0;
}

这里采用cmake组织代码,vscode进行开发。

cmake的windows下的格式如下:

# CMakeList.txt : CMake project for CMakeProject, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project ("CMakeProject")


include_directories("D:/Program Files/Polyspace/R2020a/extern/include")


link_directories("D:\\Program Files\\Polyspace\\R2020a\\extern\\lib\\win64\\microsoft")
# Add source to this project's executable.
add_executable (CMakeProject "CMakeProject.cpp" "CMakeProject.h" "MyMatrixFunctions.cpp")


# TODO: Add tests and install targets if needed.
target_link_libraries(CMakeProject mclmcrrt )

linux格式如下:

# CMakeList.txt : CMake project for CMakeProject, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)


project ("CMakeProject")


include_directories("/usr/local/Polyspace/R2020a/extern/include")




link_directories("/usr/local/Polyspace/R2020a/runtime/glnxa64")
# Add source to this project's executable.
add_executable (CMakeProject "CMakeProject.cpp" "CMakeProject.h" "MyMatrixFunctions.cpp")


# TODO: Add tests and install targets if needed.
target_link_libraries(CMakeProject mwmclmcrrt)

注意事项:

在windows如果使用动态dll库,还需要链接到lib文件,而在linux下,只需要链接到so库就好了。

程序运行

需要有安装matlab运行时电脑才可以运行上面编译出来的程序。运行时下载地址:

https://ww2.mathworks.cn/products/compiler/matlab-runtime.html

截图如下,需要和安装的matlab版本对应。

从零开始学习C++调用matlab_matlab_07

运行时安装之后,会有一个提示:

On the target computer, 
append the following to your LD_LIBRARY_PATH environment variable:
/usr/local/MATLAB/MATLAB_Runtime/v98/runtime/glnxa64
:/usr/local/MATLAB/MATLAB_Runtime/v98/bin/glnxa64
:/usr/local/MATLAB/MATLAB_Runtime/v98/sys/os/glnxa64
:/usr/local/MATLAB/MATLAB_Runtime/v98/extern/bin/glnxa64


If MATLAB Runtime is to be used with MATLAB Production
Server or MATLAB Web App Server,
you do not need to modify the above environment variable.

我们需要设置一下环境变量,才可以运行。

运行成功输出如下:

从零开始学习C++调用matlab_linux_08