Ubuntu – How to use an older version of GCC

gcc

I'm using Ubuntu 18.04.4 LTS, and I'm trying to compile a MATLAB-based program (SPM12), following their online instructions, and I believe I'm running into a problem of having a GCC version that is 'too new'.
When I reach a step in the process where my command is make && make install, I'm getting the following error message:

Warning: You are using gcc version '7.5.0'. The version of gcc is not supported. The version currently supported with MEX is '6.3.x'. For a list of currently supported compilers see: https://www.mathworks.com/support/compilers/current_release.
/usr/bin/ld: cannot find -lstdc++
collect2: error: ld returned 1 exit status

Makefile:247: recipe for target 'spm_sample_vol.mexa64' failed
make: *** [spm_sample_vol.mexa64] Error 255

Any advice would be greatly appreciated! I'm no expert, so if you could offer specific commands to follow I'd really appreciate that.
Ben

Best Answer

First of all try to find a Ubuntu release that was out after the release of the required version of GCC. You can find the release history of GCC on GCC Releases - GNU Project - Free Software Foundation (FSF) and that of Ubuntu on Ubuntu version history - Wikipedia.

GCC 6.3 was released on December 21, 2016 and the closest Ubuntu release was Ubuntu 17.04 (Zesty Zapus) which was released in April 2017. You can use the archives of Zesty to install that. But since Zesty reached end of life way back in January 2018, therefore, its archives have been moved to Old Releases. To install GCC 6.3 from its repository:

  1. Add the repository of Zesty and disable the Universe repository of Bionic since it contains 6.4 as well as 6.5 which might get installed while trying to install 6.3.

    echo "deb http://old-releases.ubuntu.com/ubuntu zesty main" | sudo tee /etc/apt/sources.list.d/zesty.list
    sudo apt-add-repository -r universe
    
  2. Update the available package information and install GCC 6.3.

    sudo apt update
    sudo apt install gcc-6
    
  3. Add GCC 6 as an alternative for GCC.

    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 1
    
  4. Check the GCC version using gcc -v. You should get the output like:

    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Ubuntu 6.3.0-12ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    Thread model: posix
    gcc version 6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2) 
    
  5. Once you're done with "making", you can remove the installed GCC 6.3 and the repository information of Zesty. And re-enable Universe repository of Bionic.

    sudo apt purge gcc-6
    sudo apt autoremove --purge
    sudo rm /etc/apt/sources.list.d/zesty.list
    sudo apt-add-repository universe
    
  6. Fix the symlink for /usr/bin/gcc.

    ln -sf /usr/bin/gcc-7 /usr/bin/gcc