By default, Ubuntu 16.04 uses gcc and g++ 5.4. This is fine for Caffe installation, and in fact there is an official guide for that. However, even the latest Matlab release 2016b only supports gcc and g++ 4.9 for mex. This becomes an issue when compiling matcaffe. Apparently this problem is met by other people as well.

I solved the issue by combining methods in this post and this post.

• Install gcc and g++ 4.9
• Change the Name="g++", ShortName="g++" in ~/.matlab/R2016b/mex_C++_glnxa64.xml to Name="g++-4.9", ShortName="g++-4.9". Same goes for gcc in mex_C_glnxa64.xml.

• Open Matlab and run

• Compile matcaffe by
• Relink libstdc++.so.6 in the Matlab directory
• Relink OpenCV related libraries in the Matlab directory