Ubuntu Linux Matlab mex

來源:互聯網
上載者:User

環境說明:Ubuntu 10.04 (LTS) AMD64 + Matlab 2011b

1. 安裝gcc g++

參考:http://stefaanlippens.net/cpp_mex_ubuntu804

zhang@ubuntu-desktop:~$ sudo apt-get install gcc-4.3 g++-4.3

2.  At the MATLAB Command Window prompt, type: 

>> 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/zhang/.matlab/R2011b 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/zhang/.matlab/R2011b.

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: /usr/local/MATLAB/R2011b/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

Overwrite /home/zhang/.matlab/R2011b/mexopts.sh ([y]/n)?
y

/usr/local/MATLAB/R2011b/bin/mexopts.sh is being copied to
/home/zhang/.matlab/R2011b/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/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
Building with the -largeArrayDims option enables the new API.
**************************************************************************
3. 警告

>> mex test.c

Warning: You are using gcc version "4.4.4-14ubuntu5)".  The version
currently supported with MEX is "4.3.4".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/

如果出現上述警告和錯誤,說明當前的Matlab版本不支援gcc 4.4。解決辦法如下。

4. 編輯檔案

zhang@ubuntu-desktop:~$ sudo gedit /usr/local/MATLAB/R2011b/bin/mexopts.sh

mexopts.sh

#----------------------------------------------------------------------------
;;
glnxa64)
#----------------------------------------------------------------------------
RPATH="-Wl,-rpath-link,$TMW_ROOT/bin/$Arch"
# StorageVersion: 1.0
# CkeyName: GNU C
# CkeyManufacturer: GNU
# CkeyLanguage: C
# CkeyVersion:
CC='gcc-4.3'
CFLAGS='-ansi -D_GNU_SOURCE'
CFLAGS="$CFLAGS -fexceptions"
CFLAGS="$CFLAGS -fPIC -fno-omit-frame-pointer -pthread"
CLIBS="$RPATH $MLIBS -lm"
COPTIMFLAGS='-O -DNDEBUG'
CDEBUGFLAGS='-g'
CLIBS="$CLIBS -lstdc++"
#
# C++keyName: GNU C++
# C++keyManufacturer: GNU
# C++keyLanguage: C++
# C++keyVersion:
CXX='g++-4.3'
CXXFLAGS='-ansi -D_GNU_SOURCE'
CXXFLAGS="$CXXFLAGS -fPIC -fno-omit-frame-pointer -pthread"
CXXLIBS="$RPATH $MLIBS -lm"
CXXOPTIMFLAGS='-O -DNDEBUG'
CXXDEBUGFLAGS='-g'
#
# FortrankeyName: gfortran
# FortrankeyManufacturer: GNU
# FortrankeyLanguage: Fortran
# FortrankeyVersion:
#
FC='gfortran-4.3'
FFLAGS='-fexceptions -fbackslash'
FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#
LD="$COMPILER"
LDEXTENSION='.mexa64'
LDFLAGS="-pthread -shared -Wl,--version-script,$TMW_ROOT/extern/lib/$Arch/$MAPFILE -Wl,--no-undefined"
LDOPTIMFLAGS='-O'
LDDEBUGFLAGS='-g'
#
POSTLINK_CMDS=':'
#----------------------------------------------------------------------------

綠色為原始部分;-4.3為增加部分。

參考:

https://help.ubuntu.com/community/MATLAB

http://ubuntuforums.org/showthread.php?p=7737533

重啟matlab

5. 測試

參見博文《C-mex執行個體》。 增加了寫mat檔案操作的.c檔案如下:

addtiontest.c

 1 #include <mex.h>
2
3 double addtiontest(double x, double y);
4 double doublesize(double a, double b);
5
6 void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[])
7 {
8 double *a;
9 double b, c;
10
11 /******Output******/
12 plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
13 a = mxGetPr(plhs[0]);
14
15 /******Input******/
16 b = *(mxGetPr(prhs[0]));
17 c = *(mxGetPr(prhs[1]));
18 *a = addtiontest(b, c);
19 }
20
21 double addtiontest(double x, double y)
22 {
23 double z;
24 z = doublesize(x,y);
25 return z;
26 }
doublesize.c

 1 #include <stdio.h>
2 #include <string.h> /* For strcmp() */
3 #include <stdlib.h> /* For EXIT_FAILURE, EXIT_SUCCESS */
4 #include <mex.h>
5 #include "mat.h"
6
7 double doublesize(double a, double b)
8 {
9 MATFile *pmat;
10 const char *file = "mattest.mat";
11
12 mxArray *mat_data;
13 mat_data = mxCreateDoubleMatrix(1,1,mxREAL);
14 memcpy((void *)(mxGetPr(mat_data)), (void *) &a, sizeof(&a));
15 printf("\n%f\n",*mxGetPr(mat_data));
16
17 int status, num;
18 pmat = matOpen(file, "w4");
19 for(num=1; num<4; num++) {
20 char pnum[] = {(char)(num+48), '\0'};
21 char pLD[13] = "LocalDouble";
22 status = matPutVariable(pmat, strcat(pLD,pnum), mat_data);
23 }
24 if (matClose(pmat) != 0) {
25 printf("Error closing file %s\n",file);
26 return(EXIT_FAILURE);
27 }
28
29 mxDestroyArray(mat_data);
30
31 double c;
32 c = 2*(a+b)+100;
33 return c;
34
35 }

在matlab command window中操作如下:

>> mex addtiontest.c doublesize.c
>> addtiontest(7.5,15)7.500000ans =   145
>> load('mattest.mat')
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.