5

g++ --version on my home machine reads 7.4.0, but a server I frequently do work on shows 4.8.5. Their servers don't support -std=c++11 compiler flags yet my home machine does. The most recent flag it does support is -std=c++0x.

However, sometimes when I write code on my home machine, compile with -std=c++0x flag without error, I will get errors if I brought it over to the server's local computers. Interestingly, SSHing to their servers does not show issues.

I'm not entirely sure what to make of this, I'm thinking my local machine may be ignoring the -std=c++0x flag because I have a newer version. Is there a way to force a specific version?

If it makes any difference, I am using Windows 10 with LXSS (Ubuntu) and the server I am connecting to is using Redhat.

gator
  • 153
  • c++11 and c++0x is same standard, so IF you configure correctly then the difference comes from compiler version. You can choose either to install 4.8.5 on WSL or write a more compatible code. Also g++4.8 should support -std=c++11 option. – Alvin Liang Nov 13 '19 at 03:51
  • @AlvinLiang it's strange, perhaps the g++ --version is specific to SSHing into the servers. -std=c++11 works through SSH and on my local machine, but does not compile when using their local machines. I'll have to verify which g++ --version it is when on their local machines. – gator Nov 13 '19 at 04:03
  • I think he means the LXSS subsystem, often called Windows Subsystem for Linux. If you install Ubuntu then it's a modified version of Ubuntu which runs without systemd and a emulated kernel layer, but retains 80% other funtions. – Alvin Liang Nov 13 '19 at 05:03
  • Possibly @AlvinLiang, but as I read it and https://askubuntu.com/help/on-topic I currently see nothing on-topic (the only Ubuntu mention is a vague 'flavor' where most people use it when being off-topic instead of giving specific details in my experience). – guiverc Nov 13 '19 at 05:20
  • @guiverc if it clarifies anything, what I typed may have been ambiguous. What I meant was not LXSS is a flavor of Ubuntu, I meant I am using Ubuntu in LXSS. In any case, I don't think it should matter how I am using Ubuntu. – gator Nov 13 '19 at 05:25

1 Answers1

6

Open the terminal in Ubuntu and type:

sudo apt install g++-4.8

This command installs g++ version 4.8.5 in Ubuntu, the same version that is installed on your server.

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 40
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 60
sudo update-alternatives --config g++

After running sudo update-alternatives --config g++ a menu of g++ versions will appear and you will be asked to select the default g++ version as follows:

Press <enter> to keep the current choice[*], or type selection number:

Input a selection number from the menu and press Enter.


To change the default version of gcc follow the same procedure except replace all instances of g++ with gcc and replace the versions of g++ with the versions of gcc.

karel
  • 114,770