这篇笔记通过mcc将.m函数编译成动态链接库供c++调用的方式实现从c++中调用matlab里面的自己写的函数。其实,调用自己编写的m函数,只是多了一步将自己的matlab函
之前在这里和这里调用了matlab自带的一些函数,是通过matlab引擎来实现的。那里调用的是matlab自带的函数,那么如果想调用自己写的.m函数该怎么办呢?其实很简单,原理类似,方法也不止一种。这篇笔记我先尝试通过mcc将.m函数编译成动态链接库供c++调用的方式。在另一篇笔记中还尝试了另一种途径:通过matlab引擎来实现。其实,调用自己编写的m函数,只是多了一步将自己的matlab函数编译成动态链接库文件(也就类似自带的那种eigen.h和libeng.so)。这里练习了一个小例子,展示从c++中调用matlab里面的自己写的函数。
实验平台:ubuntu 12.04 + g++4.6 + matlab2012a
问题描述:
有一个c++程序main.cpp,和一个matlab函数myFunc.m。现在要做这件事:
1)从main.cpp中传递2个double类型的数值a和b到myFunc.m中
2)myFunc.m中求和(sum = a+b)
3)main.cpp中接收myFunc.m返回的和并输出。
思路:
1)设置matlab的编译器,使用gcc编译器。编译m文件成.so。
2)编译.cpp文件,编译时调用.so库(添加.so的路径到编译选项)。
步骤:
1)将自己的matlab函数(myFunc.m)编译成动态链接库
(1) 设定编译器为gcc,在matlab 命令行依次执行命令mex -setup和mbuild -setup:
>> mex -setup
Options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against.
Using the 'mex -setup' command selects an options file that is
placed in ~/.matlab/R2012a and used by default for 'mex'. An options
file in the current working directory or specified on the command line
overrides the default options file in ~/.matlab/R2012a.
To override the default options file, use the 'mex -f' command
(see 'mex -help' for more information).
The options files available for mex are:
1: /opt/MATLAB/R2012a/bin/mexopts.sh :
Template Options file for building gcc MEX-files
0: Exit with no changes
Enter the number of the compiler (0-1):
1
/opt/MATLAB/R2012a/bin/mexopts.sh is being copied to ~/.matlab/R2012a/mexopts.sh
cp: cannot create regular file `~/.matlab/R2012a/mexopts.sh': Permission denied
**************************************************************************
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:
Building with the -largeArrayDims option enables the new API.
**************************************************************************
>> mbuild -setup
Options files control which compiler to use, the compiler and link command
options, and the runtime libraries to link against.
Using the 'mbuild -setup' command selects an options file that is
placed in ~/.matlab/R2012a and used by default for 'mbuild'. An options
file in the current working directory or specified on the command line
overrides the default options file in ~/.matlab/R2012a.
To override the default options file, use the 'mbuild -f' command (see 'mbuild -help' for more information).
The options files available for mbuild are:
1: /opt/MATLAB/R2012a/bin/mbuildopts.sh :
Build and link with MATLAB Compiler generated library via the system ANSI C/C++ compiler
0: Exit with no changes
Enter the number of the compiler (0-1):
1
/opt/MATLAB/R2012a/bin/mbuildopts.sh is being copied to ~/.matlab/R2012a/mbuildopts.sh
cp: cannot create regular file `~/.matlab/R2012a/mbuildopts.sh': Permission denied
>>
(2) 在matlab中,编写myFunc.m文件内容如下:
function [ C ] = myFunc(A, B)
C = A+B;
end
(3) 生成myFunc.m的动态链接库(.so)
>> mcc -W cpplib:libmyFunc -T link:lib myFunc.m -c
Warning: MATLAB Toolbox Path Cache is out of date and is not being used.
Type 'help toolbox_path_cache' for more info
>>
等待数秒,完成。可以看到myFunc.m所在的目录下生成了多个文件:
$ ls
libmyFunc.cpp libmyFunc.ctf libmyFunc.exports libmyFunc.h libmyFunc.so main.cpp mccExcludedFiles.log myFunc.m readme.txt
命令解释:其中-W是控制编译之后的封装格式;cpplib是指编译成C++的lib;cpplib冒号后面是指编译的库的名字;-T表示目标,link:lib表示要连接到一个库文件的目标,目标的名字即是.m函数的名字。-c表明需要生成.ctf文件,比如本例如果不加-c就不会生成“libmyFunc.ctf”。
生成的文件中打开“libmyFunc.h”可以看到一行:
extern LIB_libmyFunc_CPP_API void MW_CALL_CONV myFunc(int nargout, mwArray& C, const mwArray& A, const mwArray& B);
这个就是我们的myFunc.c函数待会儿在c++中调用时的接口。有4个参数,第一个是参数个数,,第二个是用来接收函数返回值的,后面2个是从c++中传递进来的变量。我们还会用到“libmyFunc.h”中的另外2个函数:libmyFuncInitialize()初始化,和注销libmyFuncTerminate()。