Sunday, January 20, 2019

software installation - How do I use the latest GCC on Ubuntu?



I want to compile my program with the latest version of gcc.



Ubuntu 14.04 comes with gcc 4.8.2, however there's 4.9.0 available, moreover, I see that it is available as a package: gcc-4.9. I tried to install it




sudo apt-get install gcc-4.9


but it says



Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'gcc-4.9-base' for regex 'gcc-4.9'
gcc-4.9-base is already the newest version.

0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


Looks like it is already installed, just not as the default one? How do I utilize it to build my program?



The best way to correctly install gcc-4.9 and set it as your default gcc version use:



sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9 g++-4.9

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9


The --slave, with g++, will cause g++ to be switched along with gcc, to the same version. But, at this point gcc-4.9 will be your only version configured in update-alternatives, so add 4.8 to update-alternatives, so there actually is an alternative, by using:



sudo apt-get install gcc-4.8 g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8


Then you can check which one that is set, and change back and forth using:




sudo update-alternatives --config gcc


NOTE: You could skip installing the PPA Repository and just use /usr/bin/gcc-4.9-base but I prefer using the fresh updated toolchains.






For GCC 5.X or 6, the packages (and correspondingly, the commands) are just called gcc-5, gcc-6, etc. This is due to the change in GCC's version scheme, where 5.1 is the first GCC 5 release, and future 5.X releases are for bug fixes.


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...