6

I want to install the latest gcc package (7.1) on Xubuntu 16.04.4. I have successfully installed the package following the instructions given here from GAD3R.

The problem is that when I run:

gcc --version

I get the following output:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

which means that my OS doesn't see the newly installed gcc as the default compiler. How can I achieve that?

Furthermore, I you have a more trusty source than the one presented in the link, please feel free to add it.

Regards

4

AFAIK the toochain-r PPA that you used is the recommended source for alternate versions of GCC. To make it the default, you can either use the update-alternatives mechanism as explained in this previous Q&A

How to use multiple instances of gcc?

or by direct symlinking as described in

Downloaded g++ 4.8 from the PPA but can't set it as default?


Although in practice it's often not necessary, since most build systems allow you to specify a particular compiler, either using command-line arguments or environment variables e.g.

CC=/usr/bin/gcc-7 ./configure

or

make CC=/usr/bin/gcc-7

or

cmake -D CMAKE_C_COMPILER=/usr/bin/gcc-7 ..

-- see for example CMake Useful Variables.

  • Great answer. Specifying the right gcc to cmake worked fine. Thank you! – Artur Barseghyan Feb 27 '19 at 22:23
3

You can use update-alternatives to make it default:

update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 60

For instance, this Dockerfile gives you Ubuntu 16.04 with gcc 7:

FROM ubuntu:16.04

RUN \
  apt-get update && \
  apt-get install -y software-properties-common && \
  add-apt-repository ppa:ubuntu-toolchain-r/test && \
  apt-get update && \
  apt-get install -y gcc-7 g++-7 && \
  update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 60 && \
  update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60
CMD /bin/bash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.