gcc4.8安装教程

一、安装方式选择

自动安装

使用 basicbench 包中的预置脚本:

1
2
# 来源:basicbench/GCC(by 侯宇)
# 将目录打包到目标机器执行自动安装脚本

手动安装

适用于需要自定义配置的场景,步骤如下:


二、手动安装步骤

1. 检查当前版本

1
gcc --version

2. 下载依赖包

GCC 4.8.2 需要以下依赖(版本要求):

  • GMP ≥ 4.2
  • MPFR ≥ 2.3.1
  • MPC ≥ 0.8.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd /tmp
# 下载GCC源码
wget https://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.bz2
tar xvjf gcc-4.8.2.tar.bz2

# 下载依赖库
wget http://10.88.162.17/software/gcc62/gmp-4.3.2.tar.bz2
tar xvjf gmp-4.3.2.tar.bz2

wget http://10.88.162.17/software/gcc62/mpc-0.8.1.tar.gz
tar xvzf mpc-0.8.1.tar.gz

wget http://10.88.162.17/software/gcc62/mpfr-2.4.2.tar.bz2
tar xvjf mpfr-2.4.2.tar.bz2

wget http://10.88.162.17/software/gcc62/binutils-2.27.tar.bz2
tar xvjf binutils-2.27.tar.bz2

3. 编译安装依赖库

按顺序安装(必须严格遵循此顺序):

3.1 GMP(GNU Multiple Precision Arithmetic Library)

1
2
3
4
5
cd /tmp/gmp-4.3.2
mkdir build && cd build
../configure --prefix=/opt/compiler/gcc-4.8.2
make -j20
make install

3.2 MPFR(Multiple Precision Floating-Point Reliable Library)

1
2
3
4
5
cd /tmp/mpfr-2.4.2
mkdir build && cd build
../configure --prefix=/opt/compiler/gcc-4.8.2 --with-gmp=/opt/compiler/gcc-4.8.2
make -j20
make install

3.3 MPC(Multiple Precision Complex Library)

1
2
3
4
5
6
7
cd /tmp/mpc-0.8.1
mkdir build && cd build
../configure --prefix=/opt/compiler/gcc-4.8.2 \
--with-gmp=/opt/compiler/gcc-4.8.2 \
--with-mpfr=/opt/compiler/gcc-4.8.2
make -j20
make install

4. 编译安装GCC

关键配置选项说明:

  • --prefix:安装路径
  • --disable-multilib:关闭跨平台支持(32/64位兼容)
  • --enable-languages:启用语言(默认all,可选c,c++,fortran等)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 设置临时库路径
export LD_LIBRARY_PATH=/opt/compiler/gcc-4.8.2/lib/:$LD_LIBRARY_PATH

cd /tmp/gcc-4.8.2
mkdir build && cd build
../configure --prefix=/opt/compiler/gcc-4.8.2 \
--with-gmp=/opt/compiler/gcc-4.8.2 \
--with-mpc=/opt/compiler/gcc-4.8.2 \
--with-mpfr=/opt/compiler/gcc-4.8.2 \
--enable-checking=release \
--enable-ld=yes \
--enable-gold=yes \
--disable-multilib
make -j20
make install

5. 编译Binutils(可选)

1
2
3
4
5
6
7
8
9
10
cd /tmp/binutils-2.27  # 注意原文拼写错误:bintuils → binutils
mkdir build && cd build
../configure --prefix=/opt/compiler/gcc-4.8.2 \
--enable-ld=yes \
--enable-gold=yes \
--with-gmp=/opt/compiler/gcc-4.8.2 \
--with-mpfr=/opt/compiler/gcc-4.8.2 \ # 注意原文拼写错误:mprf → mpfr
--with-mpc=/opt/compiler/gcc-4.8.2
make -j20
make install

三、环境配置

1. 永久生效配置

1
2
3
4
# 添加到用户profile
echo 'export PATH=/opt/compiler/gcc-4.8.2/bin/:$PATH' >> ~/.bash_profile
echo 'export LD_LIBRARY_PATH=/opt/compiler/gcc-4.8.2/lib/:/opt/compiler/gcc-4.8.2/lib64/:$LD_LIBRARY_PATH' >> ~/.bash_profile
source ~/.bash_profile

2. 软链接方案(推荐)

保留系统原GCC,通过版本化命令调用新版本:

1
2
3
ln -s /opt/compiler/gcc-4.8.2/bin/gcc /usr/local/bin/gcc482
ln -s /opt/compiler/gcc-4.8.2/bin/g++ /usr/local/bin/g++482
ln -s /opt/compiler/gcc-4.8.2/bin/gcj /usr/local/bin/gcj482

优势:可同时使用多个GCC版本,避免污染系统环境

3. 库路径补充配置

若需显式指定所有依赖库路径:

1
2
3
4
5
6
7
# 在 ~/.bash_profile 或 /etc/profile 中添加
LD_LIBRARY_PATH=/opt/compiler/gcc-4.8.2/gmp-4.3.2/lib:\
/opt/compiler/gcc-4.8.2/mpfr-2.4.2/lib:\
/opt/compiler/gcc-4.8.2/mpc-0.8.1/lib:\
/opt/compiler/gcc-4.8.2/lib:\
$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

四、验证安装

1
2
3
4
5
6
7
# 检查版本
gcc --version # 应显示 gcc (GCC) 4.8.2

# 测试编译
echo 'int main(){return 0;}' > test.c
gcc482 test.c -o test # 使用软链接命令
./test

常见问题处理

  1. 编译错误:确保依赖库安装顺序正确(GMP → MPFR → MPC)
  2. 库路径问题:临时设置 LD_LIBRARY_PATH 再编译GCC
  3. 权限问题:安装目录 /opt/compiler/ 需有写权限
  4. 拼写错误修正
    • bintuilsbinutils
    • mprfmpfr