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
You need to create a symbolic link to the gcc 4.7 library so matlab knows to use it. Something like:
ln -s {/path/to/file-name} {link-name}
If you don't want to use symbolic links, then just define this path in a terminal from which you launch matlab:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/libstdc++.so.6
./matlab