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):
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