Linux下c++调用自己编写的matlab函数:通过mcc动态链接库.so实现

12 篇文章 1 订阅
7 篇文章 1 订阅
之前在这里这里调用了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 -setupmbuild -setup

[html]  view plain  copy
  1. >> mex -setup  
  2.   
  3.     Options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against.  
  4.   
  5.     Using the 'mex -setup' command selects an options file that is  
  6.     placed in ~/.matlab/R2012a and used by default for 'mex'. An options   
  7.     file in the current working directory or specified on the command line   
  8.     overrides the default options file in ~/.matlab/R2012a.  
  9.    
  10.     To override the default options file, use the 'mex -f' command  
  11.     (see 'mex -help' for more information).  
  12.   
  13. The options files available for mex are:  
  14.   
  15.   1: /opt/MATLAB/R2012a/bin/mexopts.sh :   
  16.       Template Options file for building gcc MEX-files  
  17.    
  18.   
  19.   0: Exit with no changes  
  20.   
  21. Enter the number of the compiler (0-1):  
  22. 1  
  23.   
  24.   
  25. /opt/MATLAB/R2012a/bin/mexopts.sh is being copied to ~/.matlab/R2012a/mexopts.sh  
  26.   
  27. cp: cannot create regular file `~/.matlab/R2012a/mexopts.sh': Permission denied  
  28.   
  29. **************************************************************************  
  30.   Warning: The MATLAB C and Fortran API has changed to support MATLAB   
  31.            variables with more than 2^32-1 elements.  In the near future   
  32.            you will be required to update your code to utilize the new   
  33.            API. You can find more information about this at:   
  34.            http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html  
  35.            Building with the -largeArrayDims option enables the new API.   
  36. **************************************************************************  
  37.   
  38. >> mbuild -setup   
  39.   
  40.     Options files control which compiler to use, the compiler and link command  
  41.     options, and the runtime libraries to link against.  
  42.   
  43.     Using the 'mbuild -setup' command selects an options file that is  
  44.     placed in ~/.matlab/R2012a and used by default for 'mbuild'. An options   
  45.     file in the current working directory or specified on the command line   
  46.     overrides the default options file in ~/.matlab/R2012a.  
  47.    
  48.     To override the default options file, use the 'mbuild -f' command (see 'mbuild -help' for more information).  
  49.   
  50. The options files available for mbuild are:  
  51.   
  52.   1: /opt/MATLAB/R2012a/bin/mbuildopts.sh :   
  53.       Build and link with MATLAB Compiler generated library via the system ANSI C/C++ compiler  
  54.    
  55.   
  56.   0: Exit with no changes  
  57.   
  58. Enter the number of the compiler (0-1):  
  59. 1  
  60.   
  61.   
  62. /opt/MATLAB/R2012a/bin/mbuildopts.sh is being copied to ~/.matlab/R2012a/mbuildopts.sh  
  63.   
  64. cp: cannot create regular file `~/.matlab/R2012a/mbuildopts.sh': Permission denied  
  65. >>  

       (2) 在matlab中,编写myFunc.m文件内容如下:

[html]  view plain  copy
  1. function [ C ] = myFunc(A, B)  
  2.   
  3. C = A+B;  
  4.   
  5. end  

       (3) 生成myFunc.m的动态链接库(.so)

[html]  view plain  copy
  1. >> mcc -W cpplib:libmyFunc -T link:lib myFunc.m -c  
  2. Warning: MATLAB Toolbox Path Cache is out of date and is not being used.  
  3. Type 'help toolbox_path_cache' for more info   
  4. >>   

等待数秒,完成。可以看到myFunc.m所在的目录下生成了多个文件:

[html]  view plain  copy
  1. $ ls  
  2. 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,代码如下:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include "mclmcr.h"  
  3. #include "matrix.h"  
  4. #include "mclcppclass.h"  
  5. #include "libmyFunc.h"  
  6. #include "mclmcrrt.h"  
  7.   
  8. using namespace std;  
  9.   
  10. int main() {  
  11.   
  12.     // initialize lib,这里必须做初始化!  
  13.     if( !libmyFuncInitialize())  
  14.     {  
  15.         std::cout << "Could not initialize libmyFunc!" << std::endl;  
  16.         return -1;  
  17.     }  
  18.   
  19.     // 用户输入2个数值  
  20.     double a, b;  
  21.     cout<<"Please input 2 numbers <a b> and then press enter: "<<endl;  
  22.     cin >> a;  
  23.     cin >> b;  
  24.   
  25.     double c; //used to receive the result  
  26.   
  27.     // 为变量分配内存空间, maltab只有一种变量,就是矩阵,为了和c++变量接轨,设置成1*1的矩阵  
  28.     mwArray mwA(1, 1, mxDOUBLE_CLASS); //1,1表示矩阵的大小, mxDOUBLE_CLASS表示变量的精度  
  29.     mwArray mwB(1, 1, mxDOUBLE_CLASS);  
  30.     mwArray mwC(1, 1, mxDOUBLE_CLASS);  
  31.   
  32.     // 调用类里面的SetData函数给类赋值  
  33.     mwA.SetData(&a, 1);  
  34.     mwB.SetData(&b, 1);  
  35.   
  36.     // 调用自己的函数,求和。  
  37.     myFunc(1, mwC, mwA, mwB);  
  38.   
  39.     c = mwC.Get(1, 1);  
  40.   
  41.     cout<<"The sum is: "<<c<<endl;  
  42.   
  43.     // 后面是一些终止调用的程序  
  44.     // terminate the lib  
  45.     libmyFuncTerminate();  
  46.   
  47.     // terminate MCR  
  48.     mclTerminateApplication();  
  49.   
  50.   
  51.     return EXIT_SUCCESS;  
  52. }  

3)编译main.cpp函数,调用libmyFunc.so

[html]  view plain  copy
  1. $ 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,步骤点此

运行结果:

[html]  view plain  copy
  1. $ ./main   
  2. Warning: latest version of matlab app-defaults file not found.  
  3. Contact your system administrator to have this file installed  
  4. Please input 2 numbers <a b> and then press enter:   
  5. 10 100  
  6. The sum is: 110  
  7.   
  8. $ ./main   
  9. Warning: latest version of matlab app-defaults file not found.  
  10. Contact your system administrator to have this file installed  
  11. Please input 2 numbers <a b> and then press enter:   
  12. 1 1  
  13. 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

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
文中是linuxC++动态库 实现接口提供类导出的一个例子 注意其中使用函数返回基类指针的用法,因为Linux动态链接库不能像MFC中那样直接导出类 一、介绍 如何使用dlopen API动态地加载C++函数和类,是Unix C++程序员经常碰到的问题。 事实上,情况偶尔有些复杂,需要一些解释。这正是写这篇mini HOWTO的缘由。 理解这篇文档的前提是对C/C++语言中dlopen API有基本的了解。 这篇HOWTO的维护链接是: http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/ 二、问题所在 有时你想在运行时加载一个库(并使用其中的函数),这在你为你的程序写一些插件或模块架构的时候经常发生。 在C语言中,加载一个库轻而易举(调用dlopen、dlsym和dlclose就够了),但对C++来说,情况稍微复杂。 动态加载一个C++库的困难一部分是因为C++的name mangling (译者注:也有人把它翻译为“名字毁坏”,我觉得还是不翻译好), 另一部分是因为dlopen API是用C语言实现的,因而没有提供一个合适的方式来装载类。 在解释如何装载C++库之前,最好再详细了解一下name mangling。 我推荐您了解一下它,即使您对它不感兴趣。因为这有助于您理解问题是如何产生的,如何才能解决它们。 1. Name Mangling 在每个C++程序(或库、目标文件)中, 所有非静态(non-static)函数在二进制文件中都是以“符号(symbol)”形式出现的。 这些符号都是唯一的字符串,从而把各个函数在程序、库、目标文件中区分开来。 在C中,符号名正是函数名:strcpy函数的符号名就是“strcpy”,等等。 这可能是因为两个非静态函数的名字一定各不相同的缘故。 而C++允许重载(不同的函数有相同的名字但不同的参数), 并且有很多C所没有的特性──比如类、成员函数、异常说明──几乎不可能直接用函数名作符号名。 为了解决这个问题,C++采用了所谓的name mangling。它把函数名和一些信息(如参数数量和大小)杂糅在一起, 改造成奇形怪状,只有编译器才懂的符号名。 例如,被mangle后的foo可能看起来像foo@4%6^,或者,符号名里头甚至不包括“foo”。 其中一个问题是,C++标准(目前是[ISO14882])并没有定义名字必须如何被mangle, 所以每个编译器都按自己的方式来进行name mangling。 有些编译器甚至在不同版本间更换mangling算法(尤其是g++ 2.x和3.x)。 即使您搞清楚了您的编译器到底怎么进行mangling的,从而可以用dlsym调用函数了, 但可能仅仅限于您手头的这个编译器而已,而无法在下一版编译器下工作。 三、类 使用dlopen API的另一个问题是,它只支持加载函数。 但在C++中,您可能要用到库中的一个类,而这需要创建该类的一个实例,这不容易做到。 四、解决方案 1. extern "C" C++有个特定的关键字用来声明采用C binding的函数: extern "C" 。 用 extern "C"声明的函数将使用函数名作符号名,就像C函数一样。 因此,只有非成员函数才能被声明为extern "C",并且不能被重载。 尽管限制多多,extern "C"函数还是非常有用,因为它们可以象C函数一样被dlopen动态加载。 冠以extern "C"限定符后,并不意味着函数中无法使用C++代码了, 相反,它仍然是一个完全的C++函数,可以使用任何C++特性和各种类型的参数。
Linux调用动态库主要是通过dlopen、dlsym、dlclose等函数实现。下面是一个简单的示例: 1. 编写动态库代码 首先,我们需要编写一个动态库的代码。这里我们以一个简单的例子来说明,假设我们需要编写一个名为libmylib.so的动态库,其中包含一个用于加法运算的函数add。 // mylib.h #ifndef MYLIB_H #define MYLIB_H #ifdef __cplusplus extern "C" { #endif int add(int a, int b); #ifdef __cplusplus } #endif #endif //MYLIB_H // mylib.cpp #include "mylib.h" int add(int a, int b) { return a + b; } 注意,在动态库中需要将函数声明为extern "C",这是因为在C++函数名称会被编译器进行名称重整,而在动态库中需要使用原始的函数名称,所以需要使用extern "C"来告诉编译器不要进行名称重整。 2. 编译生成动态库 编译生成动态库的命令如下: g++ -shared -fPIC -o libmylib.so mylib.cpp 其中,-shared选项表示生成动态库,-fPIC选项表示编译时需要生成位置无关代码,-o选项表示指定输出文件名。 3. 编写调用动态库的代码 我们可以在另一个C++程序中调用上述动态库。下面是一个简单的示例: // main.cpp #include <iostream> #include <dlfcn.h> #include "mylib.h" int main() { void *handle = dlopen("./libmylib.so", RTLD_LAZY); if (!handle) { std::cerr << "Cannot open library: " << dlerror() << '\n'; return 1; } typedef int (*add_t)(int, int); add_t add_func = reinterpret_cast<add_t>(dlsym(handle, "add")); if (!add_func) { std::cerr << "Cannot load symbol add: " << dlerror() << '\n'; dlclose(handle); return 1; } int result = add_func(2, 3); std::cout << "The result is " << result << '\n'; dlclose(handle); return 0; } 该程序首先通过dlopen函数打开动态库,并将返回的句柄保存在变量handle中。然后通过dlsym函数获取动态库中的add函数地址,并将其转换为函数指针类型add_t。最后,我们可以通过add_func指针调用add函数。 注意,在调用dlsym函数时需要指定原始的函数名称,即在编写动态库代码时使用的名称。 4. 编译生成可执行程序 编译生成可执行程序的命令如下: g++ -o main main.cpp -ldl 其中,-ldl选项表示链接动态库加载器库。 5. 运行程序 运行可执行程序的命令如下: ./main 程序的输出应该是: The result is 5 至此,我们成功地调用了动态库中的函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值