Now mex in MATLAB 2012a only officially supports gcc 4.4.6 but I want to use gcc 4.7 at my own risk. Now If I compile something with mex directly, it will complain that
On Matlab R2015b, I first relinked libstdc++.so.6 and then edited .matlab7rc.sh as described above by @lindelof. On my desktop, from terminal:
locate libstdc++.so.6
In my case, the system library is located in /usr/lib64. Then
cd /usr/local/matlab/sys/os/glnxa64
mv libstdc++.so.6 libstdc++.so.6.bak
ln -s /usr/lib64/libstc++.so.6 libstc++.so.6
cd /usr/local/src/matlab/bin/glnxa64/
mv libstdc++.so.6 libstdc++.so.6.bak
ln -s /usr/lib64/libstc++.so.6 libstc++.so.6
Then edit .matlab7rc.sh in {matlabroot}/bin. Delete in the same directory any mexopts.sh file. Restart Matlab. MEX your file from scratch (this will build a new mexopts.sh file with the new settings. Run it from Matlab console.