I'm using Ubuntu 15.10, so the default g++ version is 5.2.1. Since I need to change it to the 4.7 often, I'm using update-alternatives; I've noticed, however, that the default compiler is not restored on closing the terminal; this may be an issue since there is the possibility to mix codes compiled with different compilers. Is there a way to force the shell to restore the default settings on exiting? Thank you in advice.

Edit: I wish to thank the_Seppi for his precious help

  • Don't add "Solved" to the title. – A.B. Dec 13 '15 at 11:54
  • Sorry, I wished to mark the question solved a la StackExchange way – Matteo S. Dec 13 '15 at 11:56

The alternatives system only works globally. So you could use something like

trap 'sudo update-alternatives ....' EXIT

However, this would ask for your password and also would reset it for all other active shells. So I doubt this is what you want.

My suggestion: First set the alternative back to the 'default' version of g++. Create something like the following in your ~/.bashrc file (or whatever your shell uses, when not using bash):

function oldg++() {
    alias g++=/path/to/older/g++
function newg++() {
    unalias g++

So now you'll be using the more recent g++ by default, until you run the command oldg++ This will switch to the older version for the current session or until you execute newg++, which deletes the alias and therefore switches back to the default version.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.