Peter Mitrano
Peter Mitrano

Reputation: 2270

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?

Upvotes: 1

Views: 4273

Answers (3)

Isac Casapu
Isac Casapu

Reputation: 1301

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.

Upvotes: 1

user4221
user4221

Reputation: 89

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

Upvotes: 2

Peter Mitrano
Peter Mitrano

Reputation: 2270

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?

Upvotes: 0

Related Questions