(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: http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html 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()。
2)编写main.cpp,代码如下:
#include <iostream> #include "mclmcr.h" #include "matrix.h" #include "mclcppclass.h" #include "libmyFunc.h" #include "mclmcrrt.h" using namespace std; int main() { // initialize lib,这里必须做初始化! if( !libmyFuncInitialize()) { std::cout << "Could not initialize libmyFunc!" << std::endl; return -1; } // 用户输入2个数值 double a, b; cout<<"Please input 2 numbers <a b> and then press enter: "<<endl; cin >> a; cin >> b; double c; //used to receive the result // 为变量分配内存空间, maltab只有一种变量,就是矩阵,为了和c++变量接轨,设置成1*1的矩阵 mwArray mwA(1, 1, mxDOUBLE_CLASS); //1,1表示矩阵的大小, mxDOUBLE_CLASS表示变量的精度 mwArray mwB(1, 1, mxDOUBLE_CLASS); mwArray mwC(1, 1, mxDOUBLE_CLASS); // 调用类里面的SetData函数给类赋值 mwA.SetData(&a, 1); mwB.SetData(&b, 1); // 调用自己的函数,求和。 myFunc(1, mwC, mwA, mwB); c = mwC.Get(1, 1); cout<<"The sum is: "<<c<<endl; // 后面是一些终止调用的程序 // terminate the lib libmyFuncTerminate(); // terminate MCR mclTerminateApplication(); return EXIT_SUCCESS; }
$ g++ -o main -I. -I/opt/MATLAB/R2012a/extern/include -L. -L/opt/MATLAB/R2012a/runtime/glnxa64 main.cpp -lmyFunc -lm -lmwmclmcrrt -lmwmclmcr
编译成功后,需要装一下MCR Installer,步骤点此。
运行结果:
$ ./main Warning: latest version of matlab app-defaults file not found. Contact your system administrator to have this file installed Please input 2 numbers <a b> and then press enter: 10 100 The sum is: 110 $ ./main Warning: latest version of matlab app-defaults file not found. Contact your system administrator to have this file installed Please input 2 numbers <a b> and then press enter: 1 1 The sum is: 2
源代码及编译过程中的所有文件已打包,点击这里可以下载。
参考:
http://www.oschina.net/question/565065_72598
http://blog.csdn.net/ylf13/article/details/20005347
http://developer.51cto.com/art/200909/150944.htm
http://www.cnblogs.com/xkfz007/archive/2012/02/02/2336298.html
http://www.cnblogs.com/tiandsp/archive/2013/02/26/2934070.html
http://blog.sciencenet.cn/blog-620659-579845.html
http://blog.csdn.net/nupt123456789/article/details/7463612
http://stackoverflow.com/questions/3165080/how-to-call-matlab-from-c-code