1

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?

mkierc
  • 1,193
  • 2
  • 15
  • 28
Peter Mitrano
  • 2,132
  • 28
  • 31
  • possible duplicate of [cc1plus: error: unrecognized command line option "-std=c++11" with g++](http://stackoverflow.com/questions/14674597/cc1plus-error-unrecognized-command-line-option-std-c11-with-g) – kenm Jan 06 '15 at 19:47
  • that post is about g++ 4.1, which doesn't support the std=c++11, whereas I have 4.8 which does. – Peter Mitrano Jan 06 '15 at 19:50
  • 1
    Maybe the `-ansi` is conflicting? Or maybe MATLAB isn't running the g++ you expect? – kenm Jan 06 '15 at 19:52
  • see my answer...very odd. – Peter Mitrano Jan 06 '15 at 20:00
  • That is odd. Maybe throw a `--version` into the opts to make sure it's using the g++ you think it's using? – kenm Jan 06 '15 at 20:03

3 Answers3

2

Try

mex CXXFLAGS="\$CXXFLAGS -std=c++11" simple_example.cpp

Alteratively build your mex-file without directly running Matlab such as using CMake like the following github repo : mex-it

user4221
  • 89
  • 8
1

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.

Isac Casapu
  • 1,163
  • 13
  • 21
0

It doesn't make sense, but apparently using -std=c++0x will work. I think matlab does some checking beforehand, and since it doesn't support 4.8 officially it doesn't accept it even though the compiler would. Can anyone back me up on this?

Peter Mitrano
  • 2,132
  • 28
  • 31
  • 2
    The g++ version being called by mex may not be the same as that being called from the terminal. Add `-v` to *mexopts.sh* to figure out what version is being invoked by mex. gcc4.8 definitely supports `-std=c++11` Or run `mex -setup` to see if it finds gcc4.8 – Praetorian Jan 06 '15 at 20:08