前言
在nodejs源码编译时用到了gcc升级,由于源码使用到了c++,gcc版本太低,大于6.3即可

[root@baidu1 node-v12.18.2]# ./configure
WARNING: C++ compiler (CXX=g++, 4.8.5) too old, need g++ 6.3.0 or clang++ 8.0.0
WARNING: warnings were emitted in the configure phase
INFO: configure completed successfully
[root@baidu node-v12.18.2]# gcc -v
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)

下载解压
gcc下载地址Index of /gnu/gcc,可以找到任意版本的.tar.gz

[root@baidu1 node-v12.18.2]# cd /usr/local/
#下载gcc安装包
[root@baidu1 local]# wget http://ftp.gnu.org/gnu/gcc/gcc-7.1.0/gcc-7.1.0.tar.gz
--2020-07-08 13:49:37--  http://ftp.gnu.org/gnu/gcc/gcc-7.1.0/gcc-7.1.0.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 108377377 (103M) [application/x-gzip]
...
[root@baidu1 local]# ls
bin  etc  games  gcc-7.1.0.tar.gz include  lib  lib64  libexec  sbin  share  src
[root@baidu1 local]# tar -zxvf gcc-7.1.0.tar.gz 

安装依赖包

官方提供了脚本自动化

root@baidu1 local]# cd gcc-7.1.0/
#执行gcc给出的shell,它会去下载所需4个依赖包
[root@baidu1 gcc-7.1.0]# vi ./contrib/download_prerequisites 
...
gmp='gmp-6.1.0.tar.bz2'
mpfr='mpfr-3.1.4.tar.bz2'
mpc='mpc-1.0.3.tar.gz'
isl='isl-0.16.1.tar.bz2'
 
base_url='ftp://gcc.gnu.org/pub/gcc/infrastructure/'
...
#他们的URL分别是
#ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
#ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
#ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
#ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2
[root@baidu1 gcc-7.1.0]# ./contrib/download_prerequisites
#执行后会比较慢,担心卡住可以再开个tab查看进程,它是用的wget下载
[wang@baidu1 ~]$ ps -ef |grep wget
root       3129   3126  0 13:58 pts/0    00:00:00 wget --no-verbose -O ./isl-0.16.1.tar.bz2 ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2
wang       3143   3016  0 13:58 pts/1    00:00:00 grep --color=auto wget
 
[root@baidu1 gcc-7.1.0]# ./contrib/download_prerequisites 
2020-08-01 13:42:10 URL: ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2 [2383840] -> "./gmp-6.1.0.tar.bz2" [1]
2020-08-01 13:43:23 URL: ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2 [1279284] -> "./mpfr-3.1.4.tar.bz2" [1]
2020-08-01 13:43:51 URL: ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz [669925] -> "./mpc-1.0.3.tar.gz" [1]
2020-08-01 13:45:14 URL: ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2 [1626446] -> "./isl-0.16.1.tar.bz2" [1]
gmp-6.1.0.tar.bz2: OK
mpfr-3.1.4.tar.bz2: OK
mpc-1.0.3.tar.gz: OK
isl-0.16.1.tar.bz2: OK
All prerequisites downloaded successfully.

gcc源码编译

建议先安装几个依赖包,避免返工

#新建编译目录
[root@baidu1 local]# mkdir gcc-build-7.1.0
[root@baidu1 local]# cd gcc-build-7.1.0
[root@baidu gcc-build-7.1.0]# /usr/local/gcc-7.1.0/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
//加上-j参数,-j4和-j8性能差距不大,-j16性能就比较差,make大概3-4小时
[root@baidu1 gcc-build-7.1.0]# make -j4
[root@baidu1 gcc-build-7.1.0]# make install
...
----------------------------------------------------------------------
make[4]: Nothing to be done for `install-data-am'.
make[4]: Leaving directory `/usr/local/gcc-build-7.1.0/x86_64-pc-linux-gnu/libatomic'
make[3]: Leaving directory `/usr/local/gcc-build-7.1.0/x86_64-pc-linux-gnu/libatomic'
make[2]: Leaving directory `/usr/local/gcc-build-7.1.0/x86_64-pc-linux-gnu/libatomic'
make[1]: Leaving directory `/usr/local/gcc-build-7.1.0'
[root@baidu gcc-build-7.1.0]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /usr/local/gcc-7.1.0/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 7.1.0 (GCC)

如果显示的gcc版本仍是以前的版本,就需要重启系统

校验

[root@baidu opt]# cd node-v12.18.2/
[root@baidu node-v12.18.2]# ./configure
INFO: configure completed successfully
[root@baidu node-v12.18.2]# 

升级成功。 

环境变量

cd /usr/bin
rm -rf gcc g++ c++
ln -s /usr/local/gcc10.2.0/bin/gcc gcc
ln -s /usr/local/gcc10.2.0/bin/g++ g++
ln -s /usr/local/gcc10.2.0/bin/c++ c++
最后修改:2024 年 11 月 09 日
如果觉得我的文章对你有用,请随意赞赏