So I have installed gcc-4.4 and gcc-4.3 (same for g++). Now as far as I remember there is a tool in Ubuntu which sets the symlinks for you if you just tell it which version you want. However it does not seem to work in the newest version, which I find disappointing.
root@nexus:~# update-alternatives --config gcc
update-alternatives: error: no alternatives for gcc.
root@nexus:~# update-alternatives --config cc
There is only one alternative in link group cc: /usr/bin/gcc
Nothing to configure.
root@nexus:~# dpkg -l | grep gcc | awk '{print $2}'
gcc
gcc-4.3
gcc-4.3-base
gcc-4.3-multilib
gcc-4.4
gcc-4.4-base
gcc-4.4-multilib
gcc-4.5-base
gcc-multilib
lib32gcc1
libgcc1
Any ideas?
First erase the current
update-alternatives
setup forgcc
andg++
:Install Packages
It seems that both
gcc-4.3
andgcc-4.4
are installed after install build-essential. However, we can explicitly install the following packages:Install Alternatives
Symbolic links
cc
andc++
are installed by default. We will install symbol links forgcc
andg++
, then linkcc
andc++
togcc
andg++
respectively. (Note that the10
,20
and30
options are the priorities for each alternative, where a bigger number is a higher priority.)Configure Alternatives
The last step is configuring the default commands for
gcc
,g++
. It's easy to switch between 4.3 and 4.4 interactively:Or switch using script:
execute in terminal :
Okay, so that part is fairly simple. The tricky part is that when you issue the command GCC it is actually a sybolic link to which ever version of GCC you are using. What this means is we can create a symbolic link from GCC to whichever version of GCC we want.
Is this really desirable? There are ABI changes between
gcc
versions. Compiling something with one version (eg the entire operating system) and then compiling something else with another version, can cause conflict.For example, kernel modules should always be compiled with the same version of
gcc
used to compile the kernel. With that in mind, if you manually altered the symlink between/usr/bin/gcc
and the version used in your version of Ubuntu, future DKMS-built modules might use the wronggcc
version.If you just want to build things with a different version of
gcc
, that's easy enough, even with makescripts. For example, you can pass in the version ofgcc
in theCC
environment variable:You might not need it on the make command (configure scripts usually pull it in) but it doesn't hurt.
Edit:
This assumes that you have installed the version first, with e.g.:
Original:
And here is a one-liner for those who are lazy, just change change the number at the end to the version you want. It will make the change for gcc and/or g++
In this example I switched to 4.9
There are no error checks and what not in this example, so you might want to check what will be run before you run it. Just add echo before sudo. For completeness I provide check line as well:
The output from the check should be something like:
You can check the version after with:
Semi-detailed explanation:
I was looking to switch from gcc/g++ 9.x (was installed) to 10.x:
The old version is still installed in case you need to switch back, just use the commands form 2. with your old version!
I usually configure also related gcc tools (
gcc-ar
, ...) as slaves, so you can switch all of them at once:Then to select the default one:
How about a symbolic link in a temporary directory:
mkdir x && PATH=$PWD/x:$PATH && ln -s /usr/bin/g++-7 $PWD/x/g++
You can use the alternatives command I hope it helps!
https://snipboard.io/zE4B9D.jpg