Switching gcc

How to switch the gcc version for a specific task

This article describes how you can switch the gcc version.
Let's assume, you want to use gcc-4.4.5, but your main gcc version is 4.6:

# gcc --version
gcc (Gentoo 4.6.2-r1 p1.4, pie-0.5.0) 4.6.2
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

So - what to do? The best and safest way is to switch your gcc version temporarily to the same version that is used by the upstream project.

To do that, we use the tool gcc-config. First, we'll have a look on what versions are available on your system. Install the desired version of gcc explicitly:

This list shows the versions of gcc that are now installed. The new compiler is now available and can be called as gcc-4.4.5. But the default system compiler (what you get when you call gcc) is set to 4.6.2. Like with other Funtoo tools, the asterisk marks the currently active version of gcc.

If possible, it is best to modify Makefiles and change the CC variable to force any required sources to use gcc-4.4.5 instead of gcc to build. This is a non-invasive change that will be local to your current project, which is the preferred way to make these types of build changes. This is the mechanism we use for building Enterprise kernels that build cleanly with earlier versions of gcc.

Changing Default gcc

Again, this is not recommended as it will affect the default gcc version that the entire system will use. Use this with caution. It is also not supported to run Funtoo Linux with a non-standard system compiler, sicne we are not testing it.

Before you switch your gcc version, it is very important that you make a note about what version was the active one before, so you can easily switch it back to the profiles default after you're done.

To switch the version to 4.4.5, we make use of gcc-config again. You can define the version you want to switch to either by giving the number printed before the listed version in the output of gcc-config --list-profiles or by entering the whole string. So, giving 1 or x86_64-pc-linux-gnu-4.4.5 to gcc-config leads to the same result:

Like it's already suggested in the output from above, the next and last step is to refresh your environment to make use of this switch. You may either issue the following two commands or open another login shell for this. No matter what you do, you should verify that the active gcc version is the one you wanted to switch to:

# gcc --version
gcc (Gentoo 4.6.2-r1 p1.4, pie-0.5.0) 4.6.2
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# source /etc/profile
# env-update
>>> Regenerating /etc/ld.so.cache...
# gcc --version
gcc (Gentoo 4.4.5 p1.3, pie-0.4.5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#

As you see here, the current shell is using gcc version 4.4.5 only after you have refreshed your environment. Now you can build your packages / source which need this specific version of gcc.

It is important that you reset the gcc version back to the default of your profile. To do that, simply reply the above steps again but this time, use your profile's default version of gcc to switch to.