设为首页 加入收藏

TOP

Ubuntu 14.04下用GCC及gfortran编写MEX程序(Matlab2012a)(一)
2015-12-15 23:09:09 来源: 作者: 【 】 浏览:14
Tags:Ubuntu 14.04 下用 GCC gfortran 编写 MEX 程序 Matlab2012a

一、先用apt-get安装一个低版本gcc


? ? sudo apt-get install gcc-4.4 g++-4.4 gfortran-4.4


? ? matlab2012a可以支持到最高为4.4版本的gcc,而ubuntu14.04默认的gcc版本为4.8


二、修改mexopts.sh


? ? sudo vi /opt/MATLAB/R2012a/bin/mexopts.sh


? ? 将所有的gcc、g++、gfortran出现分别替换成gcc-4.4、g++-4.4、gfortran-4.4


? ? 将CFLAGS='-ansi -D_GNU_SOURCE'改为CFLAGS='-std=c99 -D_GNU_SOURCE'以支持//风格注释


三、启动matlab执行mex -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 /home/mymotif/.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 /home/mymotif/.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/local/MATLAB/R2012a/bin/mexopts.sh :


? ? ? Template Options file for building gcc-4.4 MEX-files


? 0: Exit with no changes


Enter the number of the compiler (0-1):


1


/opt/local/MATLAB/R2012a/bin/mexopts.sh is being copied to


/home/mymotif/.matlab/R2012a/mexopts.sh


**************************************************************************


? 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.


**************************************************************************


四、测试


(1)进入matlab工作目录,并创建源文件hello1.c内容如下:


#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
? ? int i;
? ? i=mxGetScalar(prhs[0]);
? ? if(i==1)
? ? ? ? mexPrintf("hello,world!\n");
? ? else
? ? ? ? mexPrintf("大家好!\n");
}
?


>> mex hello1.c


>> hello1(0)


大家好!


>> hello1(1)


hello,world!


mexFunction 输入参数含义


int? nlhs :输出 参数的 个数


mxArray? *? plhs :输出参数的 mxArray数组


int? nrhs :输入 参数的 个数


mxArray? *? prhs: 输入参数的 mxArray 数组


(2)g++例子:


mexcpp.cpp
?
/*==========================================================
?* mexcpp.cpp - example in MATLAB External Interfaces
?*
?* Illustrates how to use some C++ language features in a MEX-file.
?* It makes use of member functions, constructors, destructors, and the
?* iostream.
?*
?* The routine simply defines a class, constructs a simple object,
?* and displays the initial values of the internal variables.? It
?* then sets the data members of the object based on the input given
?* to the MEX-file and displays the changed values.
?*
?* This file uses the extension .cpp.? Other common C++ extensions such
?* as .C, .cc, and .cxx are also supported.
?*
?* The calling syntax is:
?*
?*? ? mexcpp( num1, num2 )
?*
?* Limitations:
?* On Windows, this example uses mexPrintf instead cout.? Iostreams
?* (such as cout) are not supported with MATLAB with all C++ compilers.
?*
?* This is a MEX-file for MATLAB.
?* Copyright 1984-2009 The MathWorks, Inc.
?*
?*========================================================*/
/* $Revision: 1.5.4.4 $ */
?
#include
#include
#include "mex.h"
?
using namespace std;
?
extern void _main();
?
/****************************/
class MyData {
?
public:
? void display();
? void set_data(double v1, double v

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇iOS自定义的emoji表情键盘 下一篇C11标准的泛型机制

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: