在 Mex 中使用 C++11,在 linux 中使用 g++4.8

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

我正在尝试从 mex 脚本调用我用 C++ 11 编写的函数。 C++ 代码需要 -std=c++11,并且可以从终端正常运行。 这是 g++ -v 输出:gcc version 4.8.2 20140120 (Red Hat 4.8.2-15) (GCC) 我有适用于 Red Hat 的 Matlab 2013a。

当我第一次尝试从 matlab 控制台调用 mex filename.cpp 时,我得到:

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.

因此,我进入了 matlab 用来获取编译器选项的 /usr/local/MATLAB/R2013a/bin/mexopts.sh 文件并添加了 -std=c++11。现在我得到:

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

mex -v filename.cpp 获得的完整命令是:

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"

那么,我怎样才能正确编译它呢?

这没有意义,但显然使用 -std=c++0x 会起作用。我认为 matlab 事先做了一些检查,并且由于它不正式支持 4.8,所以即使编译器会接受它也不接受。 有人可以支持我吗?

尝试

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

或者不直接 运行 Matlab 构建你的 mex 文件,例如像下面这样使用 CMake github repo:mex-it

我的测试表明 -ansi-std=c++11 确实存在冲突,正如另一位响应者推测的那样。您可以编辑您的 mex 选项文件(例如 ~/.matlab/R2014a/mex_C++_glnxa64.xml 在我的设置中)并删除 -ansi。 另请注意,mex 接受一个 -v 标志,它会转储很多有用的调试信息。