Environment Description: Ubuntu 10.04 (LTs) amd64 + MATLAB 2011b
1. Install GCC g ++
Reference: 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. Warning
> 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/
If the preceding warnings and errors occur, the current MATLAB version does not support GCC 4.4. The solution is as follows.
4. edit the file
Zhang @ Ubuntu-desktop :~ $ Sudo gedit/usr/local/Matlab/r2011b/bin/mexopts. Sh
Mopts. 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=':'
#----------------------------------------------------------------------------
Green indicates the original part;-4.3 indicates the added part.
Refer:
Https://help.ubuntu.com/community/MATLAB
Http://ubuntuforums.org/showthread.php? P = 7737533
Restart MATLAB
5. Test
See the C-Mex instance blog. The. c file for writing mat files is added as follows:
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 }
In the MATLAB command window, perform the following operations:
>> mex addtiontest.c doublesize.c
>> addtiontest(7.5,15)7.500000ans = 145
>> load('mattest.mat')