How to choose the default gcc and g++ version? Unicorn Meta Zoo #1: Why another podcast? ...

Why does the Cisco show run command not show the full version, while the show version command does?

Does a Draconic Bloodline sorcerer's doubled proficiency bonus for Charisma checks against dragons apply to all dragon types or only the chosen one?

All ASCII characters with a given bit count

Does Prince Arnaud cause someone holding the Princess to lose?

What happened to Viserion in Season 7?

How did Elite on the NES work?

When speaking, how do you change your mind mid-sentence?

What's the difference between using dependency injection with a container and using a service locator?

What is the term for extremely loose Latin word order?

Could a cockatrice have parasitic embryos?

Page Layouts : 1 column , 2 columns-left , 2 columns-right , 3 column

What's called a person who works as someone who puts products on shelves in stores?

Why I cannot instantiate a class whose constructor is private in a friend class?

What helicopter has the most rotor blades?

In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω

Raising a bilingual kid. When should we introduce the majority language?

Suing a Police Officer Instead of the Police Department

Are there existing rules/lore for MTG planeswalkers?

What is ls Largest Number Formed by only moving two sticks in 508?

"Working on a knee"

What is a 'Key' in computer science?

Getting AggregateResult variables from Execute Anonymous Window

Is there a verb for listening stealthily?

How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?



How to choose the default gcc and g++ version?



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar ManaraDowngrade gcc from 4.8 to 4.7Change C++ versionSwitch between two version of gccHow to call a older version gcc?g++ version does not seem to change even after installationHow to make 'python' program command execute Python 3?How do I use the latest GCC on Ubuntu?Getting + installing gcc/g++ 4.9 on Ubuntu?How do I Install CUDA on Ubuntu 18.04?How to use GCC 4.7 in place of 4.6?Why is the GCC 4.7 PPA not upgrading to 4.7.1?Restoring gcc and g++ flagsC compiler cannot create excecutablesInstall a fallback version of gcc g++ alongside the latest version?“gcc --version” can't find gcc despite having g++-5 and gcc-5 installed?How to make code::blocks choose the newer version of GCCLinux kernel version and GCC version matchCan't configure custom GCC version after installationDowngrade GNU Compilers Ubuntu 18.04How could I switch OpenMPI library based on the gcc/g++ version?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







190















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?










share|improve this question




















  • 1





    As @Oli explained, this is a bad idea. Debian-devel mail list: "I don't think alternatives should be used for versioning. For example, we don't use alternatives for gcc, neither for Python." lists.debian.org/debian-devel/2014/06/msg00381.html

    – hmijail
    Aug 18 '16 at 20:39











  • OK, so what do you use for versioning?

    – WillC
    Nov 28 '18 at 8:34


















190















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?










share|improve this question




















  • 1





    As @Oli explained, this is a bad idea. Debian-devel mail list: "I don't think alternatives should be used for versioning. For example, we don't use alternatives for gcc, neither for Python." lists.debian.org/debian-devel/2014/06/msg00381.html

    – hmijail
    Aug 18 '16 at 20:39











  • OK, so what do you use for versioning?

    – WillC
    Nov 28 '18 at 8:34














190












190








190


146






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?










share|improve this question
















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?







gcc g++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 30 '18 at 8:59









Ciro Santilli 新疆改造中心996ICU六四事件

10.6k45054




10.6k45054










asked Feb 15 '11 at 15:52









NilsNils

1,111389




1,111389








  • 1





    As @Oli explained, this is a bad idea. Debian-devel mail list: "I don't think alternatives should be used for versioning. For example, we don't use alternatives for gcc, neither for Python." lists.debian.org/debian-devel/2014/06/msg00381.html

    – hmijail
    Aug 18 '16 at 20:39











  • OK, so what do you use for versioning?

    – WillC
    Nov 28 '18 at 8:34














  • 1





    As @Oli explained, this is a bad idea. Debian-devel mail list: "I don't think alternatives should be used for versioning. For example, we don't use alternatives for gcc, neither for Python." lists.debian.org/debian-devel/2014/06/msg00381.html

    – hmijail
    Aug 18 '16 at 20:39











  • OK, so what do you use for versioning?

    – WillC
    Nov 28 '18 at 8:34








1




1





As @Oli explained, this is a bad idea. Debian-devel mail list: "I don't think alternatives should be used for versioning. For example, we don't use alternatives for gcc, neither for Python." lists.debian.org/debian-devel/2014/06/msg00381.html

– hmijail
Aug 18 '16 at 20:39





As @Oli explained, this is a bad idea. Debian-devel mail list: "I don't think alternatives should be used for versioning. For example, we don't use alternatives for gcc, neither for Python." lists.debian.org/debian-devel/2014/06/msg00381.html

– hmijail
Aug 18 '16 at 20:39













OK, so what do you use for versioning?

– WillC
Nov 28 '18 at 8:34





OK, so what do you use for versioning?

– WillC
Nov 28 '18 at 8:34










6 Answers
6






active

oldest

votes


















252














First erased the current update-alternatives setup for gcc and g++:



sudo update-alternatives --remove-all gcc 
sudo update-alternatives --remove-all g++


Install Packages



It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:



sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4


Install Alternatives



Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative.)



sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc

sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++


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:



sudo update-alternatives --config gcc
sudo update-alternatives --config g++


Or switch using script:



#!/bin/sh

if [ -z "$1" ]; then
echo "usage: $0 version" 1>&2
exit 1
fi

if [ ! -f "/usr/bin/gcc-$1" ] || [ ! -f "/usr/bin/g++-$1" ]; then
echo "no such version gcc/g++ installed" 1>&2
exit 1
fi

update-alternatives --set gcc "/usr/bin/gcc-$1"
update-alternatives --set g++ "/usr/bin/g++-$1"





share|improve this answer





















  • 4





    Thx, so you have to add them manually to update-alternatives.. IIRC earlier Ubuntu versions did that automatically.

    – Nils
    Feb 15 '11 at 19:33






  • 1





    This is especially useful to me when compiling nvidia modules for different kernels. Thank you so much for explaining update-alternatives

    – earthmeLon
    Apr 15 '12 at 10:57






  • 1





    Thank you! I used your other answer to update from 4.6 to 4.7. I wanted to use this answer, but I was not sure why do you put numbers like 10 20 30 after some of you commands. Could you please explain?

    – Martin Drozdik
    Dec 28 '12 at 22:02






  • 7





    The numbers are priorities as it turns out, according to the manpage. I imagine in the case that one of the version is uninstalled it will use those priorities to determine which one should become the new default.

    – Ibrahim
    Jan 14 '13 at 20:04






  • 1





    @Ibrahim: No, they determine what gets chosen when you select auto mode

    – Cookie
    Mar 24 '14 at 16:25



















35














execute in terminal :



gcc -v
g++ -v


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.




  • You can see the symbolic link :



ls -la /usr/bin | grep gcc-4.4
ls -la /usr/bin | grep g++-4.4




  • So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:



rm /usr/bin/gcc
rm /usr/bin/g++

ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
ln -s /usr/bin/g++-4.3 /usr/bin/g++




  • Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:



ls -la /usr/bin/ | grep gcc
ls -la /usr/bin/ | grep g++




  • Finally we can check our GCC -v again and make sure we are using the correct version:



gcc -v
g++ -v






share|improve this answer


























  • Exactly what I was looking for! Forgot about the ability to symlink "gcc" to whatever version I have installed. Thanks!

    – CalebHC
    Mar 1 at 18:01



















21














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 wrong gcc 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 of gcc in the CC environment variable:



CC="gcc-4.5" ./configure
CC="gcc-4.5" make


You might not need it on the make command (configure scripts usually pull it in) but it doesn't hurt.






share|improve this answer
























  • Thx for your comment. I'm aware of the CC variable, but this was not really the question.

    – Nils
    Feb 15 '11 at 16:31






  • 2





    True enough but I have explained why gcc isn't part of the alternatives system and why this isn't particularly desirable. If neither of those are going to change your mind, just do it manually.

    – Oli
    Feb 15 '11 at 16:43











  • It was before? Now they just removed it?! Compiling (userspace) software with different gcc versions should be perfectly ok.. this discussion is getting sill..

    – Nils
    Feb 15 '11 at 18:12











  • Can you explain why per-invocation environment variables are preferable to a system-wide configuration setting? $ sudo apt-get install gcc-6 gcc-7 $ CC="gcc-7" ./configure <much output> $ make # uses gcc-7 Whereas sudo update-alternatives gcc gcc-7 would have made sure that you don't accidentally switch ABIs.

    – kfsone
    Jun 12 '17 at 22:01





















14














Edit:



This assumes that you have installed the version first, with e.g.:



sudo apt install gcc-4.9 g++-4.9


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++



ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


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:



ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do echo sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


The output from the check should be something like:



sudo ln -s -f /usr/bin/g++-4.9 /usr/bin/g++
sudo ln -s -f /usr/bin/gcc-4.9 /usr/bin/gcc
sudo ln -s -f /usr/bin/gcc-ar-4.9 /usr/bin/gcc-ar
sudo ln -s -f /usr/bin/gcc-nm-4.9 /usr/bin/gcc-nm
sudo ln -s -f /usr/bin/gcc-ranlib-4.9 /usr/bin/gcc-ranlib
sudo ln -s -f /usr/bin/x86_64-linux-gnu-g++-4.9 /usr/bin/x86_64-linux-gnu-g++
sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-4.9 /usr/bin/x86_64-linux-gnu-gcc
sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ar-4.9 /usr/bin/x86_64-linux-gnu-gcc-ar
sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-nm-4.9 /usr/bin/x86_64-linux-gnu-gcc-nm
sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ranlib-4.9 /usr/bin/x86_64-linux-gnu-gcc-ranlib


You can check the version after with:



gcc --version


Semi-detailed explanation:





  • ls -la /usr/bin/ lists all files in /usr/bin


  • | pipe (send) the output to the next command


  • grep -oP matches the search regex per line. o only shows the result not the entire matched line. P tells grep to use perl-regex. I will not go into regex here, read up on it if you want to.


  • xargs simply put, it gathers the results that are piped to it and send all of them to the end. i.e. to the command following xargs


  • bash well, it's bash. The c flag tells it to use the string as a command. In this example it loops over the arguments sent from xargs by skipping the first (0th) argument, in this case the loop skips 4.9. The 0th argument is used in the loop to change the link.


  • ln -s -f The s flag makes a symbolic link, f forces unlinking first if needed.






share|improve this answer





















  • 2





    Very old question, but this answer seems to be one of the gems we should look for when reviewing answers to old posts...

    – mook765
    Oct 16 '16 at 7:12











  • Brilliant! Best answer hands down!

    – Gabriel Staples
    Jan 15 '18 at 4:04



















0














Consider one way upgrade:




  • sudo apt install gcc-7

  • sudo apt remove gcc-5


apt will handle links staff automatically in this case. New C/C++ version is pretty compatible. You hardly would ever need old one.






share|improve this answer





















  • 1





    This did not work on Ubuntu 18.04 and gcc-7 and gcc-8. After the removal of gcc-7 there is no link for gcc.

    – Werner Henze
    Jan 11 at 11:03











  • @WernerHenze, in this case you also may need to reinstall g++ package which will update-alternatives for you.

    – Sergei
    Feb 15 at 11:40



















0














How about a symbolic link in a temporary directory:




mkdir x && PATH=$PWD/x:$PATH && ln -s /usr/bin/g++-7 $PWD/x/g++






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f26498%2fhow-to-choose-the-default-gcc-and-g-version%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    6 Answers
    6






    active

    oldest

    votes








    6 Answers
    6






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    252














    First erased the current update-alternatives setup for gcc and g++:



    sudo update-alternatives --remove-all gcc 
    sudo update-alternatives --remove-all g++


    Install Packages



    It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:



    sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4


    Install Alternatives



    Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative.)



    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

    sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
    sudo update-alternatives --set cc /usr/bin/gcc

    sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
    sudo update-alternatives --set c++ /usr/bin/g++


    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:



    sudo update-alternatives --config gcc
    sudo update-alternatives --config g++


    Or switch using script:



    #!/bin/sh

    if [ -z "$1" ]; then
    echo "usage: $0 version" 1>&2
    exit 1
    fi

    if [ ! -f "/usr/bin/gcc-$1" ] || [ ! -f "/usr/bin/g++-$1" ]; then
    echo "no such version gcc/g++ installed" 1>&2
    exit 1
    fi

    update-alternatives --set gcc "/usr/bin/gcc-$1"
    update-alternatives --set g++ "/usr/bin/g++-$1"





    share|improve this answer





















    • 4





      Thx, so you have to add them manually to update-alternatives.. IIRC earlier Ubuntu versions did that automatically.

      – Nils
      Feb 15 '11 at 19:33






    • 1





      This is especially useful to me when compiling nvidia modules for different kernels. Thank you so much for explaining update-alternatives

      – earthmeLon
      Apr 15 '12 at 10:57






    • 1





      Thank you! I used your other answer to update from 4.6 to 4.7. I wanted to use this answer, but I was not sure why do you put numbers like 10 20 30 after some of you commands. Could you please explain?

      – Martin Drozdik
      Dec 28 '12 at 22:02






    • 7





      The numbers are priorities as it turns out, according to the manpage. I imagine in the case that one of the version is uninstalled it will use those priorities to determine which one should become the new default.

      – Ibrahim
      Jan 14 '13 at 20:04






    • 1





      @Ibrahim: No, they determine what gets chosen when you select auto mode

      – Cookie
      Mar 24 '14 at 16:25
















    252














    First erased the current update-alternatives setup for gcc and g++:



    sudo update-alternatives --remove-all gcc 
    sudo update-alternatives --remove-all g++


    Install Packages



    It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:



    sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4


    Install Alternatives



    Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative.)



    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

    sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
    sudo update-alternatives --set cc /usr/bin/gcc

    sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
    sudo update-alternatives --set c++ /usr/bin/g++


    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:



    sudo update-alternatives --config gcc
    sudo update-alternatives --config g++


    Or switch using script:



    #!/bin/sh

    if [ -z "$1" ]; then
    echo "usage: $0 version" 1>&2
    exit 1
    fi

    if [ ! -f "/usr/bin/gcc-$1" ] || [ ! -f "/usr/bin/g++-$1" ]; then
    echo "no such version gcc/g++ installed" 1>&2
    exit 1
    fi

    update-alternatives --set gcc "/usr/bin/gcc-$1"
    update-alternatives --set g++ "/usr/bin/g++-$1"





    share|improve this answer





















    • 4





      Thx, so you have to add them manually to update-alternatives.. IIRC earlier Ubuntu versions did that automatically.

      – Nils
      Feb 15 '11 at 19:33






    • 1





      This is especially useful to me when compiling nvidia modules for different kernels. Thank you so much for explaining update-alternatives

      – earthmeLon
      Apr 15 '12 at 10:57






    • 1





      Thank you! I used your other answer to update from 4.6 to 4.7. I wanted to use this answer, but I was not sure why do you put numbers like 10 20 30 after some of you commands. Could you please explain?

      – Martin Drozdik
      Dec 28 '12 at 22:02






    • 7





      The numbers are priorities as it turns out, according to the manpage. I imagine in the case that one of the version is uninstalled it will use those priorities to determine which one should become the new default.

      – Ibrahim
      Jan 14 '13 at 20:04






    • 1





      @Ibrahim: No, they determine what gets chosen when you select auto mode

      – Cookie
      Mar 24 '14 at 16:25














    252












    252








    252







    First erased the current update-alternatives setup for gcc and g++:



    sudo update-alternatives --remove-all gcc 
    sudo update-alternatives --remove-all g++


    Install Packages



    It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:



    sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4


    Install Alternatives



    Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative.)



    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

    sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
    sudo update-alternatives --set cc /usr/bin/gcc

    sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
    sudo update-alternatives --set c++ /usr/bin/g++


    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:



    sudo update-alternatives --config gcc
    sudo update-alternatives --config g++


    Or switch using script:



    #!/bin/sh

    if [ -z "$1" ]; then
    echo "usage: $0 version" 1>&2
    exit 1
    fi

    if [ ! -f "/usr/bin/gcc-$1" ] || [ ! -f "/usr/bin/g++-$1" ]; then
    echo "no such version gcc/g++ installed" 1>&2
    exit 1
    fi

    update-alternatives --set gcc "/usr/bin/gcc-$1"
    update-alternatives --set g++ "/usr/bin/g++-$1"





    share|improve this answer















    First erased the current update-alternatives setup for gcc and g++:



    sudo update-alternatives --remove-all gcc 
    sudo update-alternatives --remove-all g++


    Install Packages



    It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:



    sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4


    Install Alternatives



    Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative.)



    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

    sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
    sudo update-alternatives --set cc /usr/bin/gcc

    sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
    sudo update-alternatives --set c++ /usr/bin/g++


    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:



    sudo update-alternatives --config gcc
    sudo update-alternatives --config g++


    Or switch using script:



    #!/bin/sh

    if [ -z "$1" ]; then
    echo "usage: $0 version" 1>&2
    exit 1
    fi

    if [ ! -f "/usr/bin/gcc-$1" ] || [ ! -f "/usr/bin/g++-$1" ]; then
    echo "no such version gcc/g++ installed" 1>&2
    exit 1
    fi

    update-alternatives --set gcc "/usr/bin/gcc-$1"
    update-alternatives --set g++ "/usr/bin/g++-$1"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 10 hours ago









    Olivia Stork

    1074




    1074










    answered Feb 15 '11 at 19:05









    hhlphhlp

    33k1478131




    33k1478131








    • 4





      Thx, so you have to add them manually to update-alternatives.. IIRC earlier Ubuntu versions did that automatically.

      – Nils
      Feb 15 '11 at 19:33






    • 1





      This is especially useful to me when compiling nvidia modules for different kernels. Thank you so much for explaining update-alternatives

      – earthmeLon
      Apr 15 '12 at 10:57






    • 1





      Thank you! I used your other answer to update from 4.6 to 4.7. I wanted to use this answer, but I was not sure why do you put numbers like 10 20 30 after some of you commands. Could you please explain?

      – Martin Drozdik
      Dec 28 '12 at 22:02






    • 7





      The numbers are priorities as it turns out, according to the manpage. I imagine in the case that one of the version is uninstalled it will use those priorities to determine which one should become the new default.

      – Ibrahim
      Jan 14 '13 at 20:04






    • 1





      @Ibrahim: No, they determine what gets chosen when you select auto mode

      – Cookie
      Mar 24 '14 at 16:25














    • 4





      Thx, so you have to add them manually to update-alternatives.. IIRC earlier Ubuntu versions did that automatically.

      – Nils
      Feb 15 '11 at 19:33






    • 1





      This is especially useful to me when compiling nvidia modules for different kernels. Thank you so much for explaining update-alternatives

      – earthmeLon
      Apr 15 '12 at 10:57






    • 1





      Thank you! I used your other answer to update from 4.6 to 4.7. I wanted to use this answer, but I was not sure why do you put numbers like 10 20 30 after some of you commands. Could you please explain?

      – Martin Drozdik
      Dec 28 '12 at 22:02






    • 7





      The numbers are priorities as it turns out, according to the manpage. I imagine in the case that one of the version is uninstalled it will use those priorities to determine which one should become the new default.

      – Ibrahim
      Jan 14 '13 at 20:04






    • 1





      @Ibrahim: No, they determine what gets chosen when you select auto mode

      – Cookie
      Mar 24 '14 at 16:25








    4




    4





    Thx, so you have to add them manually to update-alternatives.. IIRC earlier Ubuntu versions did that automatically.

    – Nils
    Feb 15 '11 at 19:33





    Thx, so you have to add them manually to update-alternatives.. IIRC earlier Ubuntu versions did that automatically.

    – Nils
    Feb 15 '11 at 19:33




    1




    1





    This is especially useful to me when compiling nvidia modules for different kernels. Thank you so much for explaining update-alternatives

    – earthmeLon
    Apr 15 '12 at 10:57





    This is especially useful to me when compiling nvidia modules for different kernels. Thank you so much for explaining update-alternatives

    – earthmeLon
    Apr 15 '12 at 10:57




    1




    1





    Thank you! I used your other answer to update from 4.6 to 4.7. I wanted to use this answer, but I was not sure why do you put numbers like 10 20 30 after some of you commands. Could you please explain?

    – Martin Drozdik
    Dec 28 '12 at 22:02





    Thank you! I used your other answer to update from 4.6 to 4.7. I wanted to use this answer, but I was not sure why do you put numbers like 10 20 30 after some of you commands. Could you please explain?

    – Martin Drozdik
    Dec 28 '12 at 22:02




    7




    7





    The numbers are priorities as it turns out, according to the manpage. I imagine in the case that one of the version is uninstalled it will use those priorities to determine which one should become the new default.

    – Ibrahim
    Jan 14 '13 at 20:04





    The numbers are priorities as it turns out, according to the manpage. I imagine in the case that one of the version is uninstalled it will use those priorities to determine which one should become the new default.

    – Ibrahim
    Jan 14 '13 at 20:04




    1




    1





    @Ibrahim: No, they determine what gets chosen when you select auto mode

    – Cookie
    Mar 24 '14 at 16:25





    @Ibrahim: No, they determine what gets chosen when you select auto mode

    – Cookie
    Mar 24 '14 at 16:25













    35














    execute in terminal :



    gcc -v
    g++ -v


    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.




    • You can see the symbolic link :



    ls -la /usr/bin | grep gcc-4.4
    ls -la /usr/bin | grep g++-4.4




    • So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:



    rm /usr/bin/gcc
    rm /usr/bin/g++

    ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
    ln -s /usr/bin/g++-4.3 /usr/bin/g++




    • Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:



    ls -la /usr/bin/ | grep gcc
    ls -la /usr/bin/ | grep g++




    • Finally we can check our GCC -v again and make sure we are using the correct version:



    gcc -v
    g++ -v






    share|improve this answer


























    • Exactly what I was looking for! Forgot about the ability to symlink "gcc" to whatever version I have installed. Thanks!

      – CalebHC
      Mar 1 at 18:01
















    35














    execute in terminal :



    gcc -v
    g++ -v


    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.




    • You can see the symbolic link :



    ls -la /usr/bin | grep gcc-4.4
    ls -la /usr/bin | grep g++-4.4




    • So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:



    rm /usr/bin/gcc
    rm /usr/bin/g++

    ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
    ln -s /usr/bin/g++-4.3 /usr/bin/g++




    • Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:



    ls -la /usr/bin/ | grep gcc
    ls -la /usr/bin/ | grep g++




    • Finally we can check our GCC -v again and make sure we are using the correct version:



    gcc -v
    g++ -v






    share|improve this answer


























    • Exactly what I was looking for! Forgot about the ability to symlink "gcc" to whatever version I have installed. Thanks!

      – CalebHC
      Mar 1 at 18:01














    35












    35








    35







    execute in terminal :



    gcc -v
    g++ -v


    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.




    • You can see the symbolic link :



    ls -la /usr/bin | grep gcc-4.4
    ls -la /usr/bin | grep g++-4.4




    • So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:



    rm /usr/bin/gcc
    rm /usr/bin/g++

    ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
    ln -s /usr/bin/g++-4.3 /usr/bin/g++




    • Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:



    ls -la /usr/bin/ | grep gcc
    ls -la /usr/bin/ | grep g++




    • Finally we can check our GCC -v again and make sure we are using the correct version:



    gcc -v
    g++ -v






    share|improve this answer















    execute in terminal :



    gcc -v
    g++ -v


    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.




    • You can see the symbolic link :



    ls -la /usr/bin | grep gcc-4.4
    ls -la /usr/bin | grep g++-4.4




    • So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:



    rm /usr/bin/gcc
    rm /usr/bin/g++

    ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
    ln -s /usr/bin/g++-4.3 /usr/bin/g++




    • Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:



    ls -la /usr/bin/ | grep gcc
    ls -la /usr/bin/ | grep g++




    • Finally we can check our GCC -v again and make sure we are using the correct version:



    gcc -v
    g++ -v







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 15 '11 at 17:59

























    answered Feb 15 '11 at 16:49









    hhlphhlp

    33k1478131




    33k1478131













    • Exactly what I was looking for! Forgot about the ability to symlink "gcc" to whatever version I have installed. Thanks!

      – CalebHC
      Mar 1 at 18:01



















    • Exactly what I was looking for! Forgot about the ability to symlink "gcc" to whatever version I have installed. Thanks!

      – CalebHC
      Mar 1 at 18:01

















    Exactly what I was looking for! Forgot about the ability to symlink "gcc" to whatever version I have installed. Thanks!

    – CalebHC
    Mar 1 at 18:01





    Exactly what I was looking for! Forgot about the ability to symlink "gcc" to whatever version I have installed. Thanks!

    – CalebHC
    Mar 1 at 18:01











    21














    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 wrong gcc 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 of gcc in the CC environment variable:



    CC="gcc-4.5" ./configure
    CC="gcc-4.5" make


    You might not need it on the make command (configure scripts usually pull it in) but it doesn't hurt.






    share|improve this answer
























    • Thx for your comment. I'm aware of the CC variable, but this was not really the question.

      – Nils
      Feb 15 '11 at 16:31






    • 2





      True enough but I have explained why gcc isn't part of the alternatives system and why this isn't particularly desirable. If neither of those are going to change your mind, just do it manually.

      – Oli
      Feb 15 '11 at 16:43











    • It was before? Now they just removed it?! Compiling (userspace) software with different gcc versions should be perfectly ok.. this discussion is getting sill..

      – Nils
      Feb 15 '11 at 18:12











    • Can you explain why per-invocation environment variables are preferable to a system-wide configuration setting? $ sudo apt-get install gcc-6 gcc-7 $ CC="gcc-7" ./configure <much output> $ make # uses gcc-7 Whereas sudo update-alternatives gcc gcc-7 would have made sure that you don't accidentally switch ABIs.

      – kfsone
      Jun 12 '17 at 22:01


















    21














    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 wrong gcc 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 of gcc in the CC environment variable:



    CC="gcc-4.5" ./configure
    CC="gcc-4.5" make


    You might not need it on the make command (configure scripts usually pull it in) but it doesn't hurt.






    share|improve this answer
























    • Thx for your comment. I'm aware of the CC variable, but this was not really the question.

      – Nils
      Feb 15 '11 at 16:31






    • 2





      True enough but I have explained why gcc isn't part of the alternatives system and why this isn't particularly desirable. If neither of those are going to change your mind, just do it manually.

      – Oli
      Feb 15 '11 at 16:43











    • It was before? Now they just removed it?! Compiling (userspace) software with different gcc versions should be perfectly ok.. this discussion is getting sill..

      – Nils
      Feb 15 '11 at 18:12











    • Can you explain why per-invocation environment variables are preferable to a system-wide configuration setting? $ sudo apt-get install gcc-6 gcc-7 $ CC="gcc-7" ./configure <much output> $ make # uses gcc-7 Whereas sudo update-alternatives gcc gcc-7 would have made sure that you don't accidentally switch ABIs.

      – kfsone
      Jun 12 '17 at 22:01
















    21












    21








    21







    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 wrong gcc 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 of gcc in the CC environment variable:



    CC="gcc-4.5" ./configure
    CC="gcc-4.5" make


    You might not need it on the make command (configure scripts usually pull it in) but it doesn't hurt.






    share|improve this answer













    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 wrong gcc 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 of gcc in the CC environment variable:



    CC="gcc-4.5" ./configure
    CC="gcc-4.5" make


    You might not need it on the make command (configure scripts usually pull it in) but it doesn't hurt.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 15 '11 at 16:22









    OliOli

    225k91567769




    225k91567769













    • Thx for your comment. I'm aware of the CC variable, but this was not really the question.

      – Nils
      Feb 15 '11 at 16:31






    • 2





      True enough but I have explained why gcc isn't part of the alternatives system and why this isn't particularly desirable. If neither of those are going to change your mind, just do it manually.

      – Oli
      Feb 15 '11 at 16:43











    • It was before? Now they just removed it?! Compiling (userspace) software with different gcc versions should be perfectly ok.. this discussion is getting sill..

      – Nils
      Feb 15 '11 at 18:12











    • Can you explain why per-invocation environment variables are preferable to a system-wide configuration setting? $ sudo apt-get install gcc-6 gcc-7 $ CC="gcc-7" ./configure <much output> $ make # uses gcc-7 Whereas sudo update-alternatives gcc gcc-7 would have made sure that you don't accidentally switch ABIs.

      – kfsone
      Jun 12 '17 at 22:01





















    • Thx for your comment. I'm aware of the CC variable, but this was not really the question.

      – Nils
      Feb 15 '11 at 16:31






    • 2





      True enough but I have explained why gcc isn't part of the alternatives system and why this isn't particularly desirable. If neither of those are going to change your mind, just do it manually.

      – Oli
      Feb 15 '11 at 16:43











    • It was before? Now they just removed it?! Compiling (userspace) software with different gcc versions should be perfectly ok.. this discussion is getting sill..

      – Nils
      Feb 15 '11 at 18:12











    • Can you explain why per-invocation environment variables are preferable to a system-wide configuration setting? $ sudo apt-get install gcc-6 gcc-7 $ CC="gcc-7" ./configure <much output> $ make # uses gcc-7 Whereas sudo update-alternatives gcc gcc-7 would have made sure that you don't accidentally switch ABIs.

      – kfsone
      Jun 12 '17 at 22:01



















    Thx for your comment. I'm aware of the CC variable, but this was not really the question.

    – Nils
    Feb 15 '11 at 16:31





    Thx for your comment. I'm aware of the CC variable, but this was not really the question.

    – Nils
    Feb 15 '11 at 16:31




    2




    2





    True enough but I have explained why gcc isn't part of the alternatives system and why this isn't particularly desirable. If neither of those are going to change your mind, just do it manually.

    – Oli
    Feb 15 '11 at 16:43





    True enough but I have explained why gcc isn't part of the alternatives system and why this isn't particularly desirable. If neither of those are going to change your mind, just do it manually.

    – Oli
    Feb 15 '11 at 16:43













    It was before? Now they just removed it?! Compiling (userspace) software with different gcc versions should be perfectly ok.. this discussion is getting sill..

    – Nils
    Feb 15 '11 at 18:12





    It was before? Now they just removed it?! Compiling (userspace) software with different gcc versions should be perfectly ok.. this discussion is getting sill..

    – Nils
    Feb 15 '11 at 18:12













    Can you explain why per-invocation environment variables are preferable to a system-wide configuration setting? $ sudo apt-get install gcc-6 gcc-7 $ CC="gcc-7" ./configure <much output> $ make # uses gcc-7 Whereas sudo update-alternatives gcc gcc-7 would have made sure that you don't accidentally switch ABIs.

    – kfsone
    Jun 12 '17 at 22:01







    Can you explain why per-invocation environment variables are preferable to a system-wide configuration setting? $ sudo apt-get install gcc-6 gcc-7 $ CC="gcc-7" ./configure <much output> $ make # uses gcc-7 Whereas sudo update-alternatives gcc gcc-7 would have made sure that you don't accidentally switch ABIs.

    – kfsone
    Jun 12 '17 at 22:01













    14














    Edit:



    This assumes that you have installed the version first, with e.g.:



    sudo apt install gcc-4.9 g++-4.9


    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++



    ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


    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:



    ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do echo sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


    The output from the check should be something like:



    sudo ln -s -f /usr/bin/g++-4.9 /usr/bin/g++
    sudo ln -s -f /usr/bin/gcc-4.9 /usr/bin/gcc
    sudo ln -s -f /usr/bin/gcc-ar-4.9 /usr/bin/gcc-ar
    sudo ln -s -f /usr/bin/gcc-nm-4.9 /usr/bin/gcc-nm
    sudo ln -s -f /usr/bin/gcc-ranlib-4.9 /usr/bin/gcc-ranlib
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-g++-4.9 /usr/bin/x86_64-linux-gnu-g++
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-4.9 /usr/bin/x86_64-linux-gnu-gcc
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ar-4.9 /usr/bin/x86_64-linux-gnu-gcc-ar
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-nm-4.9 /usr/bin/x86_64-linux-gnu-gcc-nm
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ranlib-4.9 /usr/bin/x86_64-linux-gnu-gcc-ranlib


    You can check the version after with:



    gcc --version


    Semi-detailed explanation:





    • ls -la /usr/bin/ lists all files in /usr/bin


    • | pipe (send) the output to the next command


    • grep -oP matches the search regex per line. o only shows the result not the entire matched line. P tells grep to use perl-regex. I will not go into regex here, read up on it if you want to.


    • xargs simply put, it gathers the results that are piped to it and send all of them to the end. i.e. to the command following xargs


    • bash well, it's bash. The c flag tells it to use the string as a command. In this example it loops over the arguments sent from xargs by skipping the first (0th) argument, in this case the loop skips 4.9. The 0th argument is used in the loop to change the link.


    • ln -s -f The s flag makes a symbolic link, f forces unlinking first if needed.






    share|improve this answer





















    • 2





      Very old question, but this answer seems to be one of the gems we should look for when reviewing answers to old posts...

      – mook765
      Oct 16 '16 at 7:12











    • Brilliant! Best answer hands down!

      – Gabriel Staples
      Jan 15 '18 at 4:04
















    14














    Edit:



    This assumes that you have installed the version first, with e.g.:



    sudo apt install gcc-4.9 g++-4.9


    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++



    ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


    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:



    ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do echo sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


    The output from the check should be something like:



    sudo ln -s -f /usr/bin/g++-4.9 /usr/bin/g++
    sudo ln -s -f /usr/bin/gcc-4.9 /usr/bin/gcc
    sudo ln -s -f /usr/bin/gcc-ar-4.9 /usr/bin/gcc-ar
    sudo ln -s -f /usr/bin/gcc-nm-4.9 /usr/bin/gcc-nm
    sudo ln -s -f /usr/bin/gcc-ranlib-4.9 /usr/bin/gcc-ranlib
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-g++-4.9 /usr/bin/x86_64-linux-gnu-g++
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-4.9 /usr/bin/x86_64-linux-gnu-gcc
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ar-4.9 /usr/bin/x86_64-linux-gnu-gcc-ar
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-nm-4.9 /usr/bin/x86_64-linux-gnu-gcc-nm
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ranlib-4.9 /usr/bin/x86_64-linux-gnu-gcc-ranlib


    You can check the version after with:



    gcc --version


    Semi-detailed explanation:





    • ls -la /usr/bin/ lists all files in /usr/bin


    • | pipe (send) the output to the next command


    • grep -oP matches the search regex per line. o only shows the result not the entire matched line. P tells grep to use perl-regex. I will not go into regex here, read up on it if you want to.


    • xargs simply put, it gathers the results that are piped to it and send all of them to the end. i.e. to the command following xargs


    • bash well, it's bash. The c flag tells it to use the string as a command. In this example it loops over the arguments sent from xargs by skipping the first (0th) argument, in this case the loop skips 4.9. The 0th argument is used in the loop to change the link.


    • ln -s -f The s flag makes a symbolic link, f forces unlinking first if needed.






    share|improve this answer





















    • 2





      Very old question, but this answer seems to be one of the gems we should look for when reviewing answers to old posts...

      – mook765
      Oct 16 '16 at 7:12











    • Brilliant! Best answer hands down!

      – Gabriel Staples
      Jan 15 '18 at 4:04














    14












    14








    14







    Edit:



    This assumes that you have installed the version first, with e.g.:



    sudo apt install gcc-4.9 g++-4.9


    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++



    ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


    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:



    ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do echo sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


    The output from the check should be something like:



    sudo ln -s -f /usr/bin/g++-4.9 /usr/bin/g++
    sudo ln -s -f /usr/bin/gcc-4.9 /usr/bin/gcc
    sudo ln -s -f /usr/bin/gcc-ar-4.9 /usr/bin/gcc-ar
    sudo ln -s -f /usr/bin/gcc-nm-4.9 /usr/bin/gcc-nm
    sudo ln -s -f /usr/bin/gcc-ranlib-4.9 /usr/bin/gcc-ranlib
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-g++-4.9 /usr/bin/x86_64-linux-gnu-g++
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-4.9 /usr/bin/x86_64-linux-gnu-gcc
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ar-4.9 /usr/bin/x86_64-linux-gnu-gcc-ar
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-nm-4.9 /usr/bin/x86_64-linux-gnu-gcc-nm
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ranlib-4.9 /usr/bin/x86_64-linux-gnu-gcc-ranlib


    You can check the version after with:



    gcc --version


    Semi-detailed explanation:





    • ls -la /usr/bin/ lists all files in /usr/bin


    • | pipe (send) the output to the next command


    • grep -oP matches the search regex per line. o only shows the result not the entire matched line. P tells grep to use perl-regex. I will not go into regex here, read up on it if you want to.


    • xargs simply put, it gathers the results that are piped to it and send all of them to the end. i.e. to the command following xargs


    • bash well, it's bash. The c flag tells it to use the string as a command. In this example it loops over the arguments sent from xargs by skipping the first (0th) argument, in this case the loop skips 4.9. The 0th argument is used in the loop to change the link.


    • ln -s -f The s flag makes a symbolic link, f forces unlinking first if needed.






    share|improve this answer















    Edit:



    This assumes that you have installed the version first, with e.g.:



    sudo apt install gcc-4.9 g++-4.9


    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++



    ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


    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:



    ls -la /usr/bin/ | grep -oP "[S]*(gcc|g++)(-[a-z]+)*[s]" | xargs bash -c 'for link in ${@:1}; do echo sudo ln -s -f "/usr/bin/${link}-${0}" "/usr/bin/${link}"; done' 4.9


    The output from the check should be something like:



    sudo ln -s -f /usr/bin/g++-4.9 /usr/bin/g++
    sudo ln -s -f /usr/bin/gcc-4.9 /usr/bin/gcc
    sudo ln -s -f /usr/bin/gcc-ar-4.9 /usr/bin/gcc-ar
    sudo ln -s -f /usr/bin/gcc-nm-4.9 /usr/bin/gcc-nm
    sudo ln -s -f /usr/bin/gcc-ranlib-4.9 /usr/bin/gcc-ranlib
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-g++-4.9 /usr/bin/x86_64-linux-gnu-g++
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-4.9 /usr/bin/x86_64-linux-gnu-gcc
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ar-4.9 /usr/bin/x86_64-linux-gnu-gcc-ar
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-nm-4.9 /usr/bin/x86_64-linux-gnu-gcc-nm
    sudo ln -s -f /usr/bin/x86_64-linux-gnu-gcc-ranlib-4.9 /usr/bin/x86_64-linux-gnu-gcc-ranlib


    You can check the version after with:



    gcc --version


    Semi-detailed explanation:





    • ls -la /usr/bin/ lists all files in /usr/bin


    • | pipe (send) the output to the next command


    • grep -oP matches the search regex per line. o only shows the result not the entire matched line. P tells grep to use perl-regex. I will not go into regex here, read up on it if you want to.


    • xargs simply put, it gathers the results that are piped to it and send all of them to the end. i.e. to the command following xargs


    • bash well, it's bash. The c flag tells it to use the string as a command. In this example it loops over the arguments sent from xargs by skipping the first (0th) argument, in this case the loop skips 4.9. The 0th argument is used in the loop to change the link.


    • ln -s -f The s flag makes a symbolic link, f forces unlinking first if needed.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 1 '17 at 12:50

























    answered Oct 15 '16 at 20:52









    AleAle

    14115




    14115








    • 2





      Very old question, but this answer seems to be one of the gems we should look for when reviewing answers to old posts...

      – mook765
      Oct 16 '16 at 7:12











    • Brilliant! Best answer hands down!

      – Gabriel Staples
      Jan 15 '18 at 4:04














    • 2





      Very old question, but this answer seems to be one of the gems we should look for when reviewing answers to old posts...

      – mook765
      Oct 16 '16 at 7:12











    • Brilliant! Best answer hands down!

      – Gabriel Staples
      Jan 15 '18 at 4:04








    2




    2





    Very old question, but this answer seems to be one of the gems we should look for when reviewing answers to old posts...

    – mook765
    Oct 16 '16 at 7:12





    Very old question, but this answer seems to be one of the gems we should look for when reviewing answers to old posts...

    – mook765
    Oct 16 '16 at 7:12













    Brilliant! Best answer hands down!

    – Gabriel Staples
    Jan 15 '18 at 4:04





    Brilliant! Best answer hands down!

    – Gabriel Staples
    Jan 15 '18 at 4:04











    0














    Consider one way upgrade:




    • sudo apt install gcc-7

    • sudo apt remove gcc-5


    apt will handle links staff automatically in this case. New C/C++ version is pretty compatible. You hardly would ever need old one.






    share|improve this answer





















    • 1





      This did not work on Ubuntu 18.04 and gcc-7 and gcc-8. After the removal of gcc-7 there is no link for gcc.

      – Werner Henze
      Jan 11 at 11:03











    • @WernerHenze, in this case you also may need to reinstall g++ package which will update-alternatives for you.

      – Sergei
      Feb 15 at 11:40
















    0














    Consider one way upgrade:




    • sudo apt install gcc-7

    • sudo apt remove gcc-5


    apt will handle links staff automatically in this case. New C/C++ version is pretty compatible. You hardly would ever need old one.






    share|improve this answer





















    • 1





      This did not work on Ubuntu 18.04 and gcc-7 and gcc-8. After the removal of gcc-7 there is no link for gcc.

      – Werner Henze
      Jan 11 at 11:03











    • @WernerHenze, in this case you also may need to reinstall g++ package which will update-alternatives for you.

      – Sergei
      Feb 15 at 11:40














    0












    0








    0







    Consider one way upgrade:




    • sudo apt install gcc-7

    • sudo apt remove gcc-5


    apt will handle links staff automatically in this case. New C/C++ version is pretty compatible. You hardly would ever need old one.






    share|improve this answer















    Consider one way upgrade:




    • sudo apt install gcc-7

    • sudo apt remove gcc-5


    apt will handle links staff automatically in this case. New C/C++ version is pretty compatible. You hardly would ever need old one.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 28 '18 at 22:24

























    answered Oct 29 '17 at 20:02









    SergeiSergei

    20115




    20115








    • 1





      This did not work on Ubuntu 18.04 and gcc-7 and gcc-8. After the removal of gcc-7 there is no link for gcc.

      – Werner Henze
      Jan 11 at 11:03











    • @WernerHenze, in this case you also may need to reinstall g++ package which will update-alternatives for you.

      – Sergei
      Feb 15 at 11:40














    • 1





      This did not work on Ubuntu 18.04 and gcc-7 and gcc-8. After the removal of gcc-7 there is no link for gcc.

      – Werner Henze
      Jan 11 at 11:03











    • @WernerHenze, in this case you also may need to reinstall g++ package which will update-alternatives for you.

      – Sergei
      Feb 15 at 11:40








    1




    1





    This did not work on Ubuntu 18.04 and gcc-7 and gcc-8. After the removal of gcc-7 there is no link for gcc.

    – Werner Henze
    Jan 11 at 11:03





    This did not work on Ubuntu 18.04 and gcc-7 and gcc-8. After the removal of gcc-7 there is no link for gcc.

    – Werner Henze
    Jan 11 at 11:03













    @WernerHenze, in this case you also may need to reinstall g++ package which will update-alternatives for you.

    – Sergei
    Feb 15 at 11:40





    @WernerHenze, in this case you also may need to reinstall g++ package which will update-alternatives for you.

    – Sergei
    Feb 15 at 11:40











    0














    How about a symbolic link in a temporary directory:




    mkdir x && PATH=$PWD/x:$PATH && ln -s /usr/bin/g++-7 $PWD/x/g++






    share|improve this answer




























      0














      How about a symbolic link in a temporary directory:




      mkdir x && PATH=$PWD/x:$PATH && ln -s /usr/bin/g++-7 $PWD/x/g++






      share|improve this answer


























        0












        0








        0







        How about a symbolic link in a temporary directory:




        mkdir x && PATH=$PWD/x:$PATH && ln -s /usr/bin/g++-7 $PWD/x/g++






        share|improve this answer













        How about a symbolic link in a temporary directory:




        mkdir x && PATH=$PWD/x:$PATH && ln -s /usr/bin/g++-7 $PWD/x/g++







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 5 '18 at 19:42









        user643722user643722

        11717




        11717






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f26498%2fhow-to-choose-the-default-gcc-and-g-version%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Why do type traits not work with types in namespace scope?What are POD types in C++?Why can templates only be...

            Will tsunami waves travel forever if there was no land?Why do tsunami waves begin with the water flowing away...

            Simple Scan not detecting my scanner (Brother DCP-7055W)Brother MFC-L2700DW printer can print, can't...