How to change the default GCC compiler in Ubuntu?

I installed gcc-3.3 / g ++ - 3.3 on ubuntu 11.04, which already has gcc / g ++ - 4.4. Therefore, both gcc-3.3 and 4.4 are available on my system. I can name both compilers as I want. If I just call the gcc command then gcc-4.4 is called. To invoke gcc-3.3, I have to use the gcc-3.3 command.

How can I change the default compiler as gcc-3.3? When I execute the gcc command, it should call gcc-3.3, not gcc-4.4.

Also, how can I change the CXX variable in the make file in gcc-3.3? I want to change one common global place in the system instead of changing all make files.

+75
gcc linux ubuntu
Oct 20 '11 at 8:09
source share
8 answers

As @Tommy suggested, you should use update-alternatives .
It assigns values ​​to each software in the family, so that it determines the order in which applications will be called.

It is used to support different versions of the same software in the system. In your case, you will be able to use several gcc declinations, and one will be approved.

To find out the current gcc priorities, enter the command marked by @tripleee comment:

 update-alternatives --query gcc 

Now notice the priority assigned to gcc-4.4 , because you need to give a higher gcc-3.3 .
To install alternatives, you should have something like this (if your gcc installation is in /usr/bin/gcc-3.3 , and gcc-4.4 priority is less than 50):

 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.3 50 

- edit -

Finally, you can also use the update-alternatives interactive interface to easily switch between versions. Type update-alternatives --config gcc to ask you to select the version of gcc that you want to use among the installed ones.

- edit 2 -

Now, to fix the CXX environment variable nationwide, you need to put the line indicated by @DipSwitch in your .bashrc (this will apply the change only for your user, which in my opinion is safer):

 echo 'export CXX=/usr/bin/gcc-3.3' >> ~/.bashrc 
+84
Oct 20 '11 at 9:45
source share

Here is a complete jHackTheRipper answer for a crowd of TL; DR. :-) In this case, I wanted to run g ++ - 4.5 on an Ubuntu system, which defaults to 4.6. As root :

 apt-get install g++-4.5 update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 100 update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.5 50 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 100 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.5 50 update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100 update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.5 50 update-alternatives --set g++ /usr/bin/g++-4.5 update-alternatives --set gcc /usr/bin/gcc-4.5 update-alternatives --set cpp-bin /usr/bin/cpp-4.5 

Here 4.6 is still the default (also known as β€œauto mode”), but I explicitly switched to 4.5 temporarily (manual mode). To return to 4.6:

 update-alternatives --auto g++ update-alternatives --auto gcc update-alternatives --auto cpp-bin 

(Note the use of cpp-bin instead of just cpp . Ubuntu already has an alternative to cpp with the main link /lib/cpp . Renaming this link will remove the /lib/cpp link, which could break scripts.)

+60
Feb 01 '12 at 21:00
source share

This is a great description and step-by-step instructions for creating and managing alternatives to master and slave (gcc and g ++).

Soon it is:

 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7 sudo update-alternatives --config gcc 
+14
Mar 12 '14 at 23:13
source share

Now for Ubuntu / exact gcc-4.9 is available.

Create a group of compiler alternatives where the compiler distribution takes precedence:

 root$ VER=4.6 ; PRIO=60 root$ update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER root$ update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-$VER $PRIO root$ VER=4.9 ; PRIO=40 root$ update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER root$ update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-$VER $PRIO 

NOTE. The g ++ version is automatically changed using the gcc version switch. cpp-bin must be run separately, since there is an alternative to "cpp".

List of available compiler alternatives:

 root$ update-alternatives --list gcc root$ update-alternatives --list cpp-bin 

To select the manual version 4.9 from gcc, g ++ and cpp, do:

 root$ update-alternatives --config gcc root$ update-alternatives --config cpp-bin 

Check the compiler version:

 root$ for i in gcc g++ cpp ; do $i --version ; done 

Restore distribution compiler settings (here: back to version 4.0):

 root$ update-alternatives --auto gcc root$ update-alternatives --auto cpp-bin 
+5
Jan 24 '15 at 14:01
source share

Between 4.8 and 6 with all --slaves :

 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 \ 10 \ --slave /usr/bin/cc cc /usr/bin/gcc-4.8 \ --slave /usr/bin/c++ c++ /usr/bin/g++-4.8 \ --slave /usr/bin/g++ g++ /usr/bin/g++-4.8 \ --slave /usr/bin/gcov gcov /usr/bin/gcov-4.8 \ --slave /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-4.8 \ --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-4.8 \ --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-4.8 \ --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-4.8 \ --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-4.8 

and

 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 \ 15 \ --slave /usr/bin/cc cc /usr/bin/gcc-6 \ --slave /usr/bin/c++ c++ /usr/bin/g++-6 \ --slave /usr/bin/g++ g++ /usr/bin/g++-6 \ --slave /usr/bin/gcov gcov /usr/bin/gcov-6 \ --slave /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-6 \ --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-6 \ --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-6 \ --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-6 \ --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-6 

Change between them update-alternatives --config gcc .

+1
May 27 '17 at 14:02
source share

If you need a faster (but still very clean) way to achieve it for personal purposes (for example, if you want to create a specific project that has some strong compiler version requirements), simply follow these steps:

  • type echo $PATH and find the personal directory with a very high priority (in my case I have ~/.local/bin );
  • add symbolic links to this directory:

For example:

 ln -s /usr/bin/gcc-WHATEVER ~/.local/bin/gcc ln -s /usr/bin/g++-WHATEVER ~/.local/bin/g++ 

Of course, this will work for one user (this is not a system solution), but, on the other hand, I do not like to change too many things in my installation.

0
Feb 28 '16 at 19:01
source share

I found this problem when trying to install a new clang compiler. It turns out that Debian and LLVM supporters agree that an alternative system should be used for alternatives, not versions.

The solution they offer looks something like this: PATH=/usr/lib/llvm-3.7/bin:$PATH
where / usr / lib / llvm -3.7 / bin is the directory that was created by the llvm-3.7 package and which contains all the tools with their names without a suffix. At the same time, llvm-config (version 3.7) appears in your PATH with its simple name. No need to guess with symbolic links and do not call llvm-config-3.7, which was installed in / usr / bin.

Also check out a package called llvm-defaults (or gcc-defaults), which may offer another way to do this (I haven't used it).

0
Aug 18 '16 at 20:34
source share

I used only the lines below and it worked. I just wanted to compile VirtualBox and VMWare WorkStation using kernel 4.8.10 on Ubuntu 14.04 . Initially, most things did not work, for example, on the graph and on the network. I am fortunate that VMWare WorkStation requested for gcc 6.2.0 . I could not start my Genymotion Android emulators because virtual boxing was disabled. If necessary, send the results later.

 VER=4.6 ; PRIO=60 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER VER=6 ; PRIO=50 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER VER=4.8 ; PRIO=40 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER 
0
Jan 10 '17 at 10:22 on
source share



All Articles