Installing newer GCC versions in Ubuntu

Installing newer GCC versions in Ubuntu

It is often useful to have installed never versions of the compiler in our systems as they often provide better performance and extended features, such as support for newer language features and better debugging information. In the case of ubuntu the new GCC stable packages can be obtained from the Ubuntu Toolchain PPA.

To install them add the ppa repository and update the APT database:

$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt-get update

Then install the desired GCC and G++ versions. At the moment of writing this page the latest available version is 7.2.0 (that correspond to the packages gcc-7 g++-7). Check if newer versions are available.

$ sudo apt-get install gcc g++ gcc-5 g++-5 gcc-6 g++-6 gcc-7 g++-7

Now the different compiler versions are available in the system by using different command names (that is, gcc-5, gcc-6, etc.), but the default version, the one that is executed with the command gcc, will still be the previous one. To change the default version executed with the gcc command we have to configure the alternatives in the system. First lets remove any previous configuration with:

$ sudo update-alternatives --remove-all gcc

Which can show the error update-alternatives: error: no alternatives for gcc if no previous alternatives were configured. The configure the alternatives modifying it accordingly to the versions available on your system. Each alternative has a priority associated with it. When a link group is in automatic mode, the alternatives pointed to by members of the group will be those which have the highest priority.

$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 90 --slave /usr/bin/g++ g++ /usr/bin/g++-5
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 80 --slave /usr/bin/g++ g++ /usr/bin/g++-6
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7

Once the different alternatives for the gcc package have been created then it can be configured with:

$ sudo update-alternatives --config gcc

And a menu similar to this will appear. Select, for example, the option 3 to set as default gcc-7 instead of gcc-5:

There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-5   90        auto mode
  1            /usr/bin/gcc-5   90        manual mode
  2            /usr/bin/gcc-6   80        manual mode
  3            /usr/bin/gcc-7   70        manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/gcc-7 to provide /usr/bin/gcc (gcc) in manual mode

Then test the version executed with the gcc command:

$ g++ -v
....
gcc version 7.2.0 (Ubuntu 7.2.0-1ubuntu1~16.04)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值