How to change the default GCC compiler in Ubuntu?

后端 未结 8 1384
孤街浪徒
孤街浪徒 2020-11-28 17:44

I have installed gcc-3.3/g++-3.3 on ubuntu 11.04 which already has gcc/g++-4.4. So in my system both gcc-3.3 and 4.4 are available. I am able to call both compilers as I wan

相关标签:
8条回答
  • 2020-11-28 18:24

    Here's a complete example of jHackTheRipper's answer for the TL;DR crowd. :-) In this case, I wanted to run g++-4.5 on an Ubuntu system that 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 (aka "auto mode"), but I explicitly switch to 4.5 temporarily (manual mode). To go back 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 a cpp alternative with a master link of /lib/cpp. Renaming that link would remove the /lib/cpp link, which could break scripts.)

    0 讨论(0)
  • 2020-11-28 18:24

    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 with update-alternatives --config gcc.

    0 讨论(0)
  • 2020-11-28 18:27

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

    It is used to maintain different versions of the same software on a system. In your case, you will be able to use several declinations of gcc, and one will be favoured.

    To figure out the current priorities of gcc, type in the command pointed out by @tripleee's comment:

    update-alternatives --query gcc
    

    Now, note the priority attributed to gcc-4.4 because you'll need to give a higher one to gcc-3.3.
    To set your alternatives, you should have something like this (assuming your gcc installation is located at /usr/bin/gcc-3.3, and gcc-4.4's 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 interactive interface of update-alternatives to easily switch between versions. Type update-alternatives --config gcc to be asked to choose the gcc version you want to use among those installed.

    --edit 2 --

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

    echo 'export CXX=/usr/bin/gcc-3.3' >> ~/.bashrc
    
    0 讨论(0)
  • 2020-11-28 18:27

    This is the great description and step-by-step instruction how to create and manage master and slave (gcc and g++) alternatives.

    Shortly it's:

    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
    
    0 讨论(0)
  • 2020-11-28 18:30

    Now, there is gcc-4.9 available for Ubuntu/precise.

    Create a group of compiler alternatives where the distro compiler has a higher priority:

    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: g++ version is changed automatically with a gcc version switch. cpp-bin has to be done separately as there exists a "cpp" master alternative.

    List available compiler alternatives:

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

    To select manually version 4.9 of gcc, g++ and cpp, do:

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

    Check compiler versions:

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

    Restore distro compiler settings (here: back to v4.6):

    root$ update-alternatives --auto gcc
    root$ update-alternatives --auto cpp-bin
    
    0 讨论(0)
  • 2020-11-28 18:36

    I found this problem while trying to install a new clang compiler. Turns out that both the Debian and the LLVM maintainers agree that the alternatives system should be used for alternatives, NOT for versioning.

    The solution they propose is something like this:
    PATH=/usr/lib/llvm-3.7/bin:$PATH
    where /usr/lib/llvm-3.7/bin is a directory that got created by the llvm-3.7 package, and which contains all the tools with their non-suffixed names. With that, llvm-config (version 3.7) appears with its plain name in your PATH. No need to muck around with symlinks, nor to call the llvm-config-3.7 that got installed in /usr/bin.

    Also, check for a package named llvm-defaults (or gcc-defaults), which might offer other way to do this (I didn't use it).

    0 讨论(0)
提交回复
热议问题