当前位置: 首页 > news >正文

网站测速工具河南省工程信息网官网

网站测速工具,河南省工程信息网官网,wordpress手机底部联系插件,图图网转自#xff1a;Linux下c调用自己编写的matlab函数#xff1a;通过mcc动态链接库.so实现_Jaster_wisdom的专栏-CSDN博客 之前在这里和这里调用了matlab自带的一些函数#xff0c;是通过matlab引擎来实现的。那里调用的是matlab自带的函数#xff0c;那么如果想调用自己写的…转自Linux下c调用自己编写的matlab函数通过mcc动态链接库.so实现_Jaster_wisdom的专栏-CSDN博客 之前在这里和这里调用了matlab自带的一些函数是通过matlab引擎来实现的。那里调用的是matlab自带的函数那么如果想调用自己写的.m函数该怎么办呢其实很简单原理类似方法也不止一种。这篇笔记我先尝试通过mcc将.m函数编译成动态链接库供c调用的方式。在另一篇笔记中还尝试了另一种途径通过matlab引擎来实现。其实调用自己编写的m函数只是多了一步将自己的matlab函数编译成动态链接库文件也就类似自带的那种eigen.h和libeng.so。这里练习了一个小例子展示从c中调用matlab里面的自己写的函数。 实验平台ubuntu 12.04 g4.6 matlab2012a 问题描述 有一个c程序main.cpp和一个matlab函数myFunc.m。现在要做这件事 1从main.cpp中传递2个double类型的数值a和b到myFunc.m中 2myFunc.m中求和sum ab 3main.cpp中接收myFunc.m返回的和并输出。 思路 1设置matlab的编译器使用gcc编译器。编译m文件成.so。 2编译.cpp文件编译时调用.so库添加.so的路径到编译选项。 步骤 1将自己的matlab函数myFunc.m编译成动态链接库 (1) 设定编译器为gcc在matlab 命令行依次执行命令mex -setup和mbuild -setup [html]  view plain  copy 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文件内容如下 [html]  view plain  copy function [ C ]  myFunc(A, B)    C  AB;    end  (3) 生成myFunc.m的动态链接库.so [html]  view plain  copy 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所在的目录下生成了多个文件 [html]  view plain  copy $ ls  libmyFunc.cpp  libmyFunc.ctf  libmyFunc.exports  libmyFunc.h  libmyFunc.so  main.cpp  mccExcludedFiles.log  myFunc.m  readme.txt  命令解释其中-W是控制编译之后的封装格式cpplib是指编译成C的libcpplib冒号后面是指编译的库的名字-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 #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;      coutPlease 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);        coutThe sum is: cendl;        // 后面是一些终止调用的程序      // terminate the lib      libmyFuncTerminate();        // terminate MCR      mclTerminateApplication();          return EXIT_SUCCESS;  }  3编译main.cpp函数调用libmyFunc.so [html]  view plain  copy $ 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 $ ./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  源代码及编译过程中的所有文件已打包点击这里可以下载。 参考 c调用matlab生成的Dll动态连接库 - OSCHINA - 中文开源技术交流社区 Linux下动态链接库的创建和使用及C调用matlab动态库问题_ylf13的专栏-CSDN博客 浅析将matlab函数编译成dll供Cpp调用的方法 - 51CTO.COM matlablinux环境中将m文件编译成动态链接库 - Mr.Rico - 博客园 matlab练习程序c/c调用matlabdll - Dsp Tian - 博客园 科学网—liuzy2011的个人资料 c调用matlab生成的Dll动态连接库_郑海波(莫川)的CSDN博客-CSDN博客_c调用matlab生成的dll How to call Matlab from C code? - Stack Overflow
http://www.pierceye.com/news/351158/

相关文章:

  • 建站网站教程视频世界网站排名
  • 做小程序的流程seo 整站优化
  • 网站前台代码国内网站主机
  • 网站后台asp源码高明顺德网站建设
  • 网站建设推广软文网络规划设计师考试全程指导(第2版) pdf
  • 备案网站多少钱支持wordpress的空间
  • 哈尔滨网页模板建站wordpress网页设定
  • 哔哩哔哩网站怎么做视频软件进入公众号会不会泄露个人信息
  • 域名过期做的网站怎么办wap网站前景
  • 网站设计公司 宁波少儿编程课
  • 建设信用卡银行积分商城网站网站关键词优化培训
  • 网站建设对电子商务的意义深圳网站设计兴田德润简介
  • 门设计的网站建设北京最大专业网站建设
  • 黄埔建网站公司长沙 网页制作
  • 网站页面海珠网站建设方案
  • 东宁网站制作公司产品彩页设计
  • 郑州大搜索网站为什么要建立网站
  • 广东网站建设公司排名网页设计模板网站免费
  • 佛山网站建设小程序注册营业执照申请
  • 网站建设文案策划鞍山兼职吧
  • 手机投资网站合肥seo优化排名公司
  • 上海网站制作公司的排名药品网站如何建设
  • 模板网站建设包括哪些wordpress怎么加关键词和描述
  • 温岭专业自适应网站建设响应式网站 模版
  • 高端包装设计优化 英语
  • 佛山新网站建设方案笔记本做网站服务器
  • c 企业网站开发杭州百度人工优化
  • 瑞安公司网站建设wordpress 主题和插件下载失败
  • 茶楼网站模板wordpress后台图
  • 做网站的流程方法wordpress 导航栏 排序