How to change the default GCC compiler in Ubuntu?

后端 未结 8 1398
孤街浪徒
孤街浪徒 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.)

提交回复
热议问题