本文介绍 update-alternatives 命令的使用方法,并以管理多版本的 gcc 和 g++ 为应用举例.

1. 简要介绍

1.1 工作原理

/etc/alternatives 存放软链接 link_name,指向实际的可执行文件 command-version-x. 在 /usr/bin 目录下存放软链接 command_name 指向 /etc/alternatives/link_name.

在命令行中输入命令

$ command_name

它执行的是

$ /usr/bin/command_name

它是软链接

/usr/bin/command_name -> /etc/alternatives/link_name

最终指向

/etc/alternatives/link_name -> /path_to/command-version-x
1.2 命令使用

查看命令帮助

$ update-alternatives --help

主要使用的命令格式如下

$ update-alternatives --install <link> <name> <path> <priority>
$ update-alternatives --install <link> <name> <path> <priority> --slave <link> <name> <path>
$ update-alternatives --display <name>
$ update-alternatives --config <name>
$ update-alternatives --set <name> <path>

2. 管理多版本 gcc 应用举例

在 Linux 系统中经常使用 gcc 搭配 g++ 编译安装源码包. 但是不同的项目可能需要不同的 gcc 版本. 并且 gcc 和 g++ 版本需要相同. 使用 update-alternatives 命令来管理 gcc 和 g++ 的多个版本.

2.1 添加 gcc 源
$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt update
2.2 应用演示

当前系统中的 gcc 和 g++ 默认为 5.4 版本.

$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

安装 4.4 版本的 gcc 和 g++.

$ sudo apt install gcc-4.4 g++-4.4

但是调用 gcc 命令时仍然使用 5.4 版本.

$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

其实系统中 4.4 和 5.4 版本的 gcc 都存在. 但是 /usr/bin/gcc 是软连接并指向了 /usr/bin/gcc-5*.

$ ll /usr/bin/gcc*
lrwxrwxrwx 1 root root     21 11月 13 21:37 /usr/bin/gcc -> /usr/bin/gcc-5*
-rwxr-xr-x 1 root root 255152 12月 14  2015 /usr/bin/gcc-4.4*
-rwxr-xr-x 1 root root 915736 10月  5  2019 /usr/bin/gcc-5*
lrwxrwxrwx 1 root root      8 11月 10 10:50 /usr/bin/gcc-ar -> gcc-ar-5*
-rwxr-xr-x 1 root root  31136 10月  5  2019 /usr/bin/gcc-ar-5*
lrwxrwxrwx 1 root root      8 11月 10 10:50 /usr/bin/gcc-nm -> gcc-nm-5*
-rwxr-xr-x 1 root root  31136 10月  5  2019 /usr/bin/gcc-nm-5*
lrwxrwxrwx 1 root root     12 11月 10 10:50 /usr/bin/gcc-ranlib -> gcc-ranlib-5*
-rwxr-xr-x 1 root root  31136 10月  5  2019 /usr/bin/gcc-ranlib-5*

update-alternatives 命令的 --install 用来添加版本条目.

$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 44
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 54

使用 --display显示条目下的版本信息.

$ sudo update-alternatives --display gcc
gcc - auto mode
  link best version is /usr/bin/gcc-5
  link currently points to /usr/bin/gcc-5
  link gcc is /usr/bin/gcc
/usr/bin/gcc-4.4 - priority 44
/usr/bin/gcc-5 - priority 54

也可使用 --list 显示版本信息.

$ sudo update-alternatives --list gcc
/usr/bin/gcc-4.4
/usr/bin/gcc-5

可以使用 --set 设定命令的默认版本.

$ sudo update-alternatives --set gcc /usr/bin/gcc-4.4 
update-alternatives: using /usr/bin/gcc-4.4 to provide /usr/bin/gcc (gcc) in manual mode
$ gcc --version
gcc (Ubuntu/Linaro 4.4.7-8ubuntu7) 4.4.7
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

更多地,使用 --config 设定命令的默认版本.

$ sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-5     54        auto mode
* 1            /usr/bin/gcc-4.4   44        manual mode
  2            /usr/bin/gcc-5     54        manual mode

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

使用 --remove 删除命令的一个版本条目.

$ sudo update-alternatives --remove gcc /usr/bin/gcc-4.4
$ sudo update-alternatives --display gcc
gcc - manual mode
  link best version is /usr/bin/gcc-5
  link currently points to /usr/bin/gcc-5
  link gcc is /usr/bin/gcc
/usr/bin/gcc-5 - priority 54

--remove-all 则会删除全部版本条目.

$ sudo update-alternatives --remove-all gcc
$ sudo update-alternatives --display gcc
update-alternatives: error: no alternatives for gcc

一般 gcc 和 g++ 版本须一致,使用 --slave 可以使得改变 gcc 的默认版本时自动改变 g++ 的版本.

$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 44 --slave /usr/bin/g++ g++ /usr/bin/g++-4.4
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 54 --slave /usr/bin/g++ g++ /usr/bin/g++-5
$ sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-5     54        auto mode
  1            /usr/bin/gcc-4.4   44        manual mode
  2            /usr/bin/gcc-5     54        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/gcc-4.4 to provide /usr/bin/gcc (gcc) in manual mode
$ gcc --version
gcc (Ubuntu/Linaro 4.4.7-8ubuntu7) 4.4.7
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ --version
g++ (Ubuntu/Linaro 4.4.7-8ubuntu7) 4.4.7
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Logo

更多推荐