安装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 |
目前系统为ubuntu21.04,gcc 10.3,安装cuda 11.3后编译cuda samples会报错,例如 chrono模块、gcc版本过高等提示。
多版本gcc共存、随时切换
第一步,先使用命令 sudo apt install gcc-7 g++-7
安装 gcc 7.5,成功安装后可以在/user/bin/下看到两个版本:
ubuntu@ubuntu:~$ ls -l /usr/bin/gcc*
lrwxrwxrwx 1 root root 21 Oct 27 02:26 /usr/bin/gcc -> /etc/alternatives/gcc
lrwxrwxrwx 1 root root 23 Apr 8 2021 /usr/bin/gcc-10 -> x86_64-linux-gnu-gcc-10
lrwxrwxrwx 1 root root 22 Nov 26 2020 /usr/bin/gcc-7 -> x86_64-linux-gnu-gcc-7
lrwxrwxrwx 1 root root 9 Apr 12 2021 /usr/bin/gcc-ar -> gcc-ar-10
lrwxrwxrwx 1 root root 26 Apr 8 2021 /usr/bin/gcc-ar-10 -> x86_64-linux-gnu-gcc-ar-10
lrwxrwxrwx 1 root root 25 Nov 26 2020 /usr/bin/gcc-ar-7 -> x86_64-linux-gnu-gcc-ar-7
lrwxrwxrwx 1 root root 9 Apr 12 2021 /usr/bin/gcc-nm -> gcc-nm-10
lrwxrwxrwx 1 root root 26 Apr 8 2021 /usr/bin/gcc-nm-10 -> x86_64-linux-gnu-gcc-nm-10
lrwxrwxrwx 1 root root 25 Nov 26 2020 /usr/bin/gcc-nm-7 -> x86_64-linux-gnu-gcc-nm-7
lrwxrwxrwx 1 root root 13 Apr 12 2021 /usr/bin/gcc-ranlib -> gcc-ranlib-10
lrwxrwxrwx 1 root root 30 Apr 8 2021 /usr/bin/gcc-ranlib-10 -> x86_64-linux-gnu-gcc-ranlib-10
lrwxrwxrwx 1 root root 29 Nov 26 2020 /usr/bin/gcc-ranlib-7 -> x86_64-linux-gnu-gcc-ranlib-7
第二步,配置gcc版本的优先级,默认使用的gcc版本为优先级最高的。设置gcc 10优先级为100,设置gcc 7优先级为70。那么默认使用gcc10。
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7
第三步,切换需要的gcc版本,命令为sudo update-alternatives --config gcc
,默认是0选项。输入需要的gcc版本序号后,即可成功切换。
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/gcc-10 100 auto mode
1 /usr/bin/gcc-10 100 manual mode
2 /usr/bin/gcc-7 70 manual mode
Press <enter> to keep the current choice[*], or type selection number: