Search code examples
matlabc++11mexg++4.8

using C++11 in Mex with g++ 4.8 in linux


I'm trying to call a function I wrote in C++ 11 from a mex script. The C++ code requires -std=c++11, and runs fine from the terminal. Here's g++ -v output: gcc version 4.8.2 20140120 (Red Hat 4.8.2-15) (GCC) I have Matlab 2013a for Red Hat.

When I first tried calling mex filename.cpp from matlab console I got:

This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

So, I went into the /usr/local/MATLAB/R2013a/bin/mexopts.sh file that matlab uses to get compler options and added -std=c++11. Now I get:

cc1plus: error: unrecognized command line option "-std=c++11"

The full command gotten from mex -v filename.cpp is:

g++ -c -I/usr/local/MATLAB/R2013a/extern/include -I/usr/local/MATLAB/R2013a/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread -std=c++11  -DMX_COMPAT_32 -O -DNDEBUG  "mexMorph.cpp"

So, how can I get this to compile properly?


Solution

  • My testing indicates that -ansi and -std=c++11 do conflict, as another responder has speculated. You could edit your mex options file (e.g. ~/.matlab/R2014a/mex_C++_glnxa64.xml in my setup) and remove -ansi. Also note that mex accepts a -v flag, which dumps a lot of useful debugging info.