gcc、g++不同版本的安装和切换(Installation and switching of different versions of GCC and G + +)

参考:https://blog.csdn.net/FontThrone/article/details/104279224

1. 可以通过如下命令查看当前安装的版本:

ll /usr/bin/gcc*

2. 安装gcc-4.8 和gcc-5:

sudo apt install gcc-4.8 gcc-4.8-multilib g++-4.8 g++-4.8-multilib
sudo apt install gcc-5 gcc-5--multilib g++-5 g++-5--multilib

3. 使用update-alternatives设置gcc和g++:

update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令、哪个软件版本。
其中40 ,50 ,70是优先级数值可以自己设定,–slave能保证gcc和g++保持相同的版本。

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50 --slave /usr/bin/g++ g++ /usr/bin/g++-5
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7

4. 使用如下命令选择gcc的版本:

# sudo update-alternatives --config gcc
# 可以看到当前gcc默认的版本是gcc-7,下面我们修改为gcc-4.8,直接选择编号即可。
$ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-7     70        auto mode
  1            /usr/bin/gcc-4.8   40        manual mode
  2            /usr/bin/gcc-5     50        manual mode
  3            /usr/bin/gcc-7     70        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/gcc-4.8 to provide /usr/bin/gcc (gcc) in manual mode

5. 验证是否修改成功:

gcc -v
g++ -v

6. 删除

删除某个gcc版本的选项的话,可以使用

再使用apt-remove删除即可

sudo update-alternatives --remove gcc /usr/bin/gcc-4.5
————————

参考:https://blog.csdn.net/FontThrone/article/details/104279224

1. You can view the currently installed version through the following command:

ll /usr/bin/gcc*

2. Install gcc-4.8 and gcc-5:

sudo apt install gcc-4.8 gcc-4.8-multilib g++-4.8 g++-4.8-multilib
sudo apt install gcc-5 gcc-5--multilib g++-5 g++-5--multilib

3. 使用update-alternatives设置gcc和g++:

Update alternatives is a special tool for maintaining the system command linker in Ubuntu system. Through it, you can easily set which command and which software version the system uses by default.
Among them, 40, 50 and 70 are priority values, which can be set by yourself. The – slave can ensure that GCC and G + + maintain the same version.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50 --slave /usr/bin/g++ g++ /usr/bin/g++-5
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7

4. Use the following command to select the version of GCC:

# sudo update-alternatives --config gcc
# 可以看到当前gcc默认的版本是gcc-7,下面我们修改为gcc-4.8,直接选择编号即可。
$ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-7     70        auto mode
  1            /usr/bin/gcc-4.8   40        manual mode
  2            /usr/bin/gcc-5     50        manual mode
  3            /usr/bin/gcc-7     70        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/gcc-4.8 to provide /usr/bin/gcc (gcc) in manual mode

5. Verify whether the modification is successful:

gcc -v
g++ -v

6. Delete

If you delete the option of a GCC version, you can use
Then use apt remove to delete

sudo update-alternatives --remove gcc /usr/bin/gcc-4.5