GCC 降级 / 版本选择

2024-09-07 13:18:39 浏览数 (2)

不同应用可能需要不同版本的 gcc,而且可能遇到gcc 某些版本无法安装的情况,本文记录相关内容。

背景

安装cuda开发库之后,使用gcc编译会可能会报错要求gcc版本,如下表

CUDA version

max supported GCC version

11.1, 11.2, 11.3

10

11

9

10.1, 10.2

8

9.2, 10.0

7

9.0, 9.1

6

8

5.3

7

4.9

5.5, 6

4.8

4.2, 5

4.6

4.1

4.5

4.0

4.4

我当前 gcc 的版本为 11.4, 但是需要安装 gcc 7的版本,但是安装时报错:

代码语言:txt复制
> sudo apt install gcc-7 g  -7

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package g  -7 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Package gcc-7 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'gcc-7' has no installation candidate
E: Package 'g  -7' has no installation candidate

而且同时我需要兼容保留多个 gcc 版本.

gcc 版本降级

遇到上述错误,原因为:使用的apt镜像库没有这个软件。

解决方案

添加 apt 库

代码语言:txt复制
sudo vim /etc/apt/sources.list

在最后添加

代码语言:txt复制
# stable add by , in order to install g  7
deb [arch=amd64] http://archive.ubuntu.com/ubuntu focal main universe

更新 apt 库

代码语言:txt复制
sudo apt update

重新安装 gcc g

代码语言:txt复制
sudo apt install gcc-7 g  -7

此时已经可以安装了

gcc 版本维护

使用 update-alternatives 设置 gcc 和 g

update-alternatives 是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令、哪个软件版本。

  • 查看当前 gcc g 版本
代码语言:txt复制
ll /usr/bin/gcc*
lrwxrwxrwx 1 root root  6 Aug  5  2021 /usr/bin/gcc -> gcc-11
lrwxrwxrwx 1 root root 23 May 13  2023 /usr/bin/gcc-11 -> x86_64-linux-gnu-gcc-11
lrwxrwxrwx 1 root root 23 May 13  2023 /usr/bin/gcc-12 -> x86_64-linux-gnu-gcc-12
lrwxrwxrwx 1 root root 22 Mar 26  2020 /usr/bin/gcc-7 -> x86_64-linux-gnu-gcc-7
lrwxrwxrwx 1 root root  9 Aug  5  2021 /usr/bin/gcc-ar -> gcc-ar-11
lrwxrwxrwx 1 root root 26 May 13  2023 /usr/bin/gcc-ar-11 -> x86_64-linux-gnu-gcc-ar-11
lrwxrwxrwx 1 root root 26 May 13  2023 /usr/bin/gcc-ar-12 -> x86_64-linux-gnu-gcc-ar-12
lrwxrwxrwx 1 root root 25 Mar 26  2020 /usr/bin/gcc-ar-7 -> x86_64-linux-gnu-gcc-ar-7
lrwxrwxrwx 1 root root  9 Aug  5  2021 /usr/bin/gcc-nm -> gcc-nm-11
lrwxrwxrwx 1 root root 26 May 13  2023 /usr/bin/gcc-nm-11 -> x86_64-linux-gnu-gcc-nm-11
lrwxrwxrwx 1 root root 26 May 13  2023 /usr/bin/gcc-nm-12 -> x86_64-linux-gnu-gcc-nm-12
lrwxrwxrwx 1 root root 25 Mar 26  2020 /usr/bin/gcc-nm-7 -> x86_64-linux-gnu-gcc-nm-7
lrwxrwxrwx 1 root root 13 Aug  5  2021 /usr/bin/gcc-ranlib -> gcc-ranlib-11
lrwxrwxrwx 1 root root 30 May 13  2023 /usr/bin/gcc-ranlib-11 -> x86_64-linux-gnu-gcc-ranlib-11
lrwxrwxrwx 1 root root 30 May 13  2023 /usr/bin/gcc-ranlib-12 -> x86_64-linux-gnu-gcc-ranlib-12
lrwxrwxrwx 1 root root 29 Mar 26  2020 /usr/bin/gcc-ranlib-7 -> x86_64-linux-gnu-gcc-ranlib-7

gcc 已经安装的版本为 12, 11, 7

代码语言:txt复制
> ll /usr/bin/g  *
lrwxrwxrwx 1 root root  6 Aug  5  2021 /usr/bin/g   -> g  -11
lrwxrwxrwx 1 root root 23 May 13  2023 /usr/bin/g  -11 -> x86_64-linux-gnu-g  -11
lrwxrwxrwx 1 root root 23 May 13  2023 /usr/bin/g  -12 -> x86_64-linux-gnu-g  -12
lrwxrwxrwx 1 root root 22 Mar 26  2020 /usr/bin/g  -7 -> x86_64-linux-gnu-g  -7

g 已经安装的版本为 12, 11, 7

  • 配置 gcc g 的版本优先级
代码语言:txt复制
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 40 --slave /usr/bin/g   g   /usr/bin/g  -12
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50 --slave /usr/bin/g   g   /usr/bin/g  -11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 --slave /usr/bin/g   g   /usr/bin/g  -7

其中40 ,50 ,60是优先级数值可以自己设定,–slave 能保证 gcc 和 g 保持相同的版本。

  • update-alternatives 配置 gcc 版本
代码语言:txt复制
sudo update-alternatives --config gcc
代码语言:txt复制
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-7    60        auto mode
  1            /usr/bin/gcc-11   50        manual mode
  2            /usr/bin/gcc-12   40        manual mode
  3            /usr/bin/gcc-7    60        manual mode

Press <enter> to keep the current choice[*], or type selection number: 
  • 此时查看 gcc 的版本,可以看到已经变成 7.5.0 的版本
代码语言:txt复制
> gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.5.0-6ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c  ,go,brig,d,fortran,objc,obj-c   --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-6ubuntu2) 
  • 版本切换
代码语言:txt复制
sudo update-alternatives --config gcc

命令执行后,选择 1, 2, 或 3 可以选定当前使用的 gcc g 版本,比如我选择了 1

1 的前面会多个星星 *

代码语言:txt复制
  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-7    60        auto mode
* 1            /usr/bin/gcc-11   50        manual mode
  2            /usr/bin/gcc-12   40        manual mode
  3            /usr/bin/gcc-7    60        manual mode

此时 gcc 版本为 11

代码语言:txt复制
> gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c  ,go,brig,d,fortran,objc,obj-c  ,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)

g 版本也为 11

代码语言:txt复制
> g   -v
Using built-in specs.
COLLECT_GCC=g  
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c  ,go,brig,d,fortran,objc,obj-c  ,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 

参考资料

  • https://blog.csdn.net/wanggao_1990/article/details/120989070
  • https://blog.csdn.net/FontThrone/article/details/104279224

文章链接:

https://cloud.tencent.com/developer/article/2449856

0 人点赞