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
sudo apt-get install gcc-4.9 g++-4.9 gcc-4.9-multilib g++-4.9-multilib
  • 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

mex -setup C -f ~/.matlab/R2016b/mex_C_glnxa64.xml
mex -setup C++ -f ~/.matlab/R2016b/mex_C++_glnxa64.xml
  • Compile matcaffe by
make matcaffe
  • Relink libstdc++.so.6 in the Matlab directory
cd /usr/local/MATLAB/R2016b/sys/os/glnxa64
sudo rm libstdc++.so.6
sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 libstdc++.so.6
  • Relink OpenCV related libraries in the Matlab directory
cd /usr/local/MATLAB/R2016b/bin/glnxa64
sudo rm libopencv_core.so.2.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9 libopencv_core.so.2.4
sudo rm libopencv_highgui.so.2.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9 libopencv_highgui.so.2.4
sudo rm libopencv_imgproc.so.2.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.9 libopencv_imgproc.so.2.4