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 <iostream>
#include <math.h>
#include "mex.h"
using namespace std;
extern void _main();
/****************************/
class MyData {
public:
void display();
void set_data(double v1, double v2);
MyData(double v1 = 0, double v2 = 0);
~MyData() { }
private:
double val1, val2;
};
MyData::MyData(double v1, double v2)
{
val1 = v1;
val2 = v2;
}
void MyData::display()
{
#ifdef _WIN32
mexPrintf("Value1 = %g\n", val1);
mexPrintf("Value2 = %g\n\n", val2);
#else
cout << "Value1 = " << val1 << "\n";
cout << "Value2 = " << val2 << "\n\n";
#endif
}
void MyData::set_data(double v1, double v2) { val1 = v1; val2 = v2; }
/*********************/
static
void mexcpp(
double num1,
double num2
)
{
#ifdef _WIN32
mexPrintf("\nThe initialized data in object:\n");
#else
cout << "\nThe initialized data in object:\n";
#endif
MyData *d = new MyData; // Create a MyData object
d->display(); // It should be initialized to
// zeros
d->set_data(num1,num2); // Set data members to incoming
// values
#ifdef _WIN32
mexPrintf("After setting the object's data to your input:\n");
#else
cout << "After setting the object's data to your input:\n";
#endif
d->display(); // Make sure the set_data() worked
delete(d);
flush(cout);
return;
}
void mexFunction(
int nlhs,
mxArray *[],
int nrhs,
const mxArray *prhs[]
)
{
double *vin1, *vin2;
/* Check for proper number of arguments */
if (nrhs != 2) {
mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin",
"MEXCPP requires two input arguments.");
} else if (nlhs >= 1) {
mexErrMsgIdAndTxt("MATLAB:mexcpp:nargout",
"MEXCPP requires no output argument.");
}
vin1 = (double *) mxGetPr(prhs[0]);
vin2 = (double *) mxGetPr(prhs[1]);
mexcpp(*vin1, *vin2);
return;
}
>> mex mexcpp.cpp
>> mexcpp(2,4)
The initialized data in object:
Value1 = 0
Value2 = 0
After setting the object's data to your input:
Value1 = 2
Value2 = 4
(3)gfortran
# include <fintrf.h>
subroutine mexFunction ( nlhs, plhs, nrhs, prhs )
c*********************************************************************72
c
cc MEXFUNCTION is a MATLAB/F77 interface for the factorial function.
c
c Discussion:
c
c The MATLAB user types
c
c integer plhs(*), prhs(*)
c integer nlhs, nrhs
integer nlhs
integer nrhs
mwpointer plhs(nlhs)
mwpointer prhs(nrhs)
call mexPrintf('Hello MATLAB World!')
return
end
>> mex mexHelloWorld.F
>> mexHelloWorld
Hello MATLAB World!>>
>>
Ubuntu下GCC、G++和gfortran版本切換 http://www.linuxidc.com/Linux/2015-09/123461.htm
Linux升級GCC 4.8.1清晰簡明教程(Ubuntu 12.04 64位版為例) http://www.linuxidc.com/Linux/2014-04/99583.htm
在CentOS 6.4中編譯安裝GCC 4.8.1 + GDB 7.6.1 + Eclipse 在CentOS 6.4中編譯安裝GCC 4.8.1 + GDB 7.6.1 + Eclipse
Ubuntu下Vim+GCC+GDB安裝及使用 http://www.linuxidc.com/Linux/2013-01/78159.htm
Ubuntu下兩個GCC版本切換 http://www.linuxidc.com/Linux/2012-10/72284.htm
CentOS6.5升級手動安裝GCC4.8.2 http://www.linuxidc.com/Linux/2015-01/112595.htm
GCC 的詳細介紹:請點這裡
GCC 的下載地址:請點這裡
更多Ubuntu相關信息見Ubuntu 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=2