怎么在2024年给CentOS7的gcc升级

出于某种原因,我需要在2024年使用CentOS7,而且还要给它自带的gcc升级。网上的教程们,比如这个,多数要用到yum命令在线下载,但是CentOS7的软件源在2024年早就被关停了,我在网上找了好多镜像,也没有结果,总是报这个错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Could not retrieve mirrorlist http://mirrorlist.centos.org?arch=x86_64&release=7&repo=sclo-rh error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"


One of the configured repositories failed (未知),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:

1. Contact the upstream for the repository and get them to fix the problem.

2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).

3. Run the command with the repository temporarily disabled
yum --disablerepo=<repoid> ...

4. Disable the repository permanently, so yum won't use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:

yum-config-manager --disable <repoid>
or
subscription-manager repos --disable=<repoid>

5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:

yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: centos-sclo-rh/x86_64

于是我不得不在不使用yum的情况下安装gcc。

首先,获取软件包。在gcc的官网上选择你需要的版本。注意,如果你选择了太新的版本,有编译不通过的可能。在这里,我选择的是gcc-6.1.0

选择好了以后,下载所需的软件包,.tar.gz格式即可。当然,你也可以在准备保存下载文件的文件夹处打开终端,输入命令:

1
wget http://ftp.gnu.org/gnu/gcc/gcc-6.1.0/gcc-6.1.0.tar.gz

然后使用tar命令解压:

1
tar -xzvf gcc-6.1.0.tar.gz

解压完成后进入解压得到的文件夹

1
cd gcc-6.1.0

用脚本安装依赖

1
./contrib/download_prerequisites

建立并进入一个用来编译的文件夹

1
2
mkdir gcc-build-6.1.0
cd gcc-build-6.1.0

初始化编译设置,生成makefile文件

1
../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib

开始编译。这一步耗时会非常长,我在虚拟机上用了6个小时。

1
make

安装

1
make install

重启电脑,检查gcc版本:

1
gcc --version

输出

1
2
3
4
gcc (GCC) 6.1.0
Copyright © 2016 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

说明你更新好了。


怎么在2024年给CentOS7的gcc升级
https://suzumiyaakizuki.github.io/2024/08/15/怎么在2024年给CentOS7的gcc升级/
作者
SuzumiyaAkizuki
发布于
2024年8月15日
许可协议