406 lines
8.4 KiB
Markdown
406 lines
8.4 KiB
Markdown
|
||
|
||
|
||
---
|
||
|
||
# 🧭 GCC 13.3.0 从源码构建指南(适用于 Kylin Linux Advanced Server V10 / RHEL 系)
|
||
|
||
目标:在 `/opt/gcc-13` 构建一套完全独立的新 GCC,不覆盖系统自带版本。
|
||
附带可选 `/opt/binutils-2.40`,用于提升链接器兼容性。
|
||
|
||
---
|
||
|
||
## 一、系统准备
|
||
|
||
### 1️⃣ 更新系统并安装构建依赖
|
||
|
||
```bash
|
||
sudo dnf groupinstall "Development Tools" -y
|
||
sudo dnf install -y \
|
||
gcc gcc-c++ make bison flex texinfo git wget curl xz gawk perl python3 \
|
||
glibc-devel glibc-headers libstdc++-devel zlib-devel \
|
||
gmp-devel mpfr-devel libmpc-devel isl-devel
|
||
```
|
||
|
||
> 若系统仓库的 `gmp/mpfr/mpc/isl` 太旧,也没关系,下面会采用 **内联构建**。
|
||
|
||
---
|
||
|
||
## 二、可选但推荐:升级 binutils (2.40)
|
||
|
||
### 2️⃣ 构建并安装
|
||
|
||
```bash
|
||
cd soft
|
||
wget https://mirrors.aliyun.com/gnu/binutils/binutils-2.40.tar.gz
|
||
tar -xzf binutils-2.40.tar.gz
|
||
mkdir build-binutils-2.40 && cd build-binutils-2.40
|
||
|
||
../binutils-2.40/configure \
|
||
--prefix=/usr/local/binutils-2.40 \
|
||
--disable-multilib \
|
||
--enable-gold \
|
||
--enable-ld=default \
|
||
--enable-plugins
|
||
|
||
make -j"$(nproc)"
|
||
make install
|
||
```
|
||
|
||
### 3️⃣ 激活新版 binutils
|
||
|
||
```bash
|
||
export PATH=/usr/local/binutils-2.40/bin:$PATH
|
||
```
|
||
|
||
验证:
|
||
|
||
```bash
|
||
ld --version | head -1
|
||
as --version | head -1
|
||
```
|
||
|
||
输出应含 “2.40”。
|
||
|
||
> ✅ 若系统已有较新 binutils,可跳过本节。
|
||
|
||
---
|
||
|
||
## 三、下载并准备 GCC 13.3.0 源码
|
||
|
||
### 4️⃣ 获取源码
|
||
|
||
```bash
|
||
cd soft
|
||
wget https://mirrors.aliyun.com/gnu/gcc/gcc-13.3.0/gcc-13.3.0.tar.xz
|
||
tar -xf gcc-13.3.0.tar.xz
|
||
cd gcc-13.3.0
|
||
```
|
||
|
||
### 5️⃣ 下载依赖库(推荐)
|
||
|
||
```bash
|
||
./contrib/download_prerequisites
|
||
```
|
||
|
||
该脚本会自动下载合适的:
|
||
|
||
- GMP
|
||
|
||
- MPFR
|
||
|
||
- MPC
|
||
|
||
- ISL
|
||
|
||
|
||
> 若离线环境,可在其他机器下载后放到以下目录:
|
||
>
|
||
> ```
|
||
> gcc-13.3.0/gmp/
|
||
> gcc-13.3.0/mpfr/
|
||
> gcc-13.3.0/mpc/
|
||
> gcc-13.3.0/isl/
|
||
> ```
|
||
|
||
---
|
||
|
||
```
|
||
cat > contrib/download_prerequisites <<'EOF'
|
||
#!/bin/sh
|
||
# Modified for Aliyun GNU mirror + official ISL source (SourceForge)
|
||
# Author: windyboy setup helper
|
||
|
||
set -e
|
||
|
||
gmp='gmp-6.2.1.tar.bz2'
|
||
mpfr='mpfr-4.1.0.tar.bz2'
|
||
mpc='mpc-1.2.1.tar.gz'
|
||
isl='isl-0.24.tar.bz2'
|
||
|
||
fetch='wget -c --no-check-certificate'
|
||
|
||
# 阿里云 GNU 镜像基础路径
|
||
base="https://mirrors.aliyun.com/gnu"
|
||
|
||
download() {
|
||
pkg=$1
|
||
url=$2
|
||
echo "==> Downloading $pkg"
|
||
${fetch} "$url" -O "$pkg" || {
|
||
echo "❌ Failed: $pkg"
|
||
exit 1
|
||
}
|
||
}
|
||
|
||
download "$gmp" "$base/gmp/$gmp"
|
||
download "$mpfr" "$base/mpfr/$mpfr"
|
||
download "$mpc" "$base/mpc/$mpc"
|
||
download "$isl" "https://libisl.sourceforge.io/$isl"
|
||
|
||
echo "==> Extracting..."
|
||
for ar in $gmp $mpfr $mpc $isl; do
|
||
tar -xf "$ar"
|
||
ln -sf "${ar%.tar*}" "${ar%-*}"
|
||
done
|
||
|
||
echo "✅ All prerequisites downloaded and extracted successfully (Aliyun + SourceForge)."
|
||
EOF
|
||
|
||
chmod +x contrib/download_prerequisites
|
||
```
|
||
|
||
|
||
## 四、构建并安装 GCC 13.3.0
|
||
|
||
### 6️⃣ 创建构建目录(out-of-tree)
|
||
|
||
```bash
|
||
cd soft
|
||
mkdir build-gcc-13 && cd build-gcc-13
|
||
```
|
||
|
||
### 7️⃣ 配置参数
|
||
|
||
```bash
|
||
../gcc-13.3.0/configure \
|
||
--prefix=/usr/local/gcc-13 \
|
||
--enable-languages=c,c++ \
|
||
--disable-multilib \
|
||
--enable-checking=release \
|
||
--enable-lto \
|
||
--enable-threads=posix \
|
||
--with-system-zlib
|
||
```
|
||
|
||
|
||
说明:
|
||
|
||
- `--disable-multilib`:仅构建 64 位版,节省空间、避免冲突
|
||
|
||
- `--enable-lto`:启用 Link Time Optimization
|
||
|
||
- `--enable-threads=posix`:标准线程支持
|
||
|
||
- `--with-system-zlib`:若安装了 zlib-devel
|
||
|
||
|
||
---
|
||
|
||
### 8️⃣ 编译与安装
|
||
|
||
```bash
|
||
make -j"$(nproc)"
|
||
make install
|
||
```
|
||
|
||
💡 **提示:**
|
||
|
||
- 首次编译耗时较久(15–60 分钟)。
|
||
|
||
- 若出现 OOM,可降并行度:
|
||
|
||
```bash
|
||
sudo make -j4
|
||
```
|
||
|
||
|
||
---
|
||
|
||
## 五、启用与测试新 GCC
|
||
|
||
### 9️⃣ 临时启用
|
||
|
||
```bash
|
||
export PATH=/usr/local/gcc-13/bin:$PATH
|
||
export LD_LIBRARY_PATH=/usr/local/gcc-13/lib64:$LD_LIBRARY_PATH
|
||
```
|
||
|
||
### 🔟 验证版本
|
||
|
||
```bash
|
||
gcc -v
|
||
g++ -v
|
||
```
|
||
|
||
应显示:
|
||
|
||
```
|
||
gcc version 13.3.0 (GCC)
|
||
```
|
||
|
||
测试代码:
|
||
|
||
```bash
|
||
cat > hello.cpp <<'EOF'
|
||
#include <iostream>
|
||
int main() { std::cout << "Hello GCC " << __VERSION__ << std::endl; }
|
||
EOF
|
||
g++ -std=c++20 hello.cpp -O2 -o hello
|
||
./hello
|
||
```
|
||
|
||
输出应为:
|
||
|
||
```
|
||
Hello GCC 13.3.0
|
||
```
|
||
|
||
---
|
||
|
||
## 六、长期使用与并存管理
|
||
|
||
### 11️⃣ 持久化环境变量
|
||
|
||
**用户级:**
|
||
|
||
```bash
|
||
echo 'export PATH=/opt/gcc-13/bin:$PATH' >> ~/.bashrc
|
||
echo 'export LD_LIBRARY_PATH=/opt/gcc-13/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
|
||
source ~/.bashrc
|
||
```
|
||
|
||
**系统级(所有用户生效):**
|
||
|
||
```bash
|
||
sudo tee /etc/profile.d/gcc13.sh <<'EOF'
|
||
export PATH=/opt/gcc-13/bin:$PATH
|
||
export LD_LIBRARY_PATH=/opt/gcc-13/lib64:$LD_LIBRARY_PATH
|
||
EOF
|
||
sudo chmod +x /etc/profile.d/gcc13.sh
|
||
```
|
||
|
||
---
|
||
|
||
### 12️⃣ 运行时库兼容性(关键)
|
||
|
||
若运行你的程序时报:
|
||
|
||
```
|
||
GLIBCXX_3.x.y not found
|
||
```
|
||
|
||
说明运行期加载了系统旧版 libstdc++。
|
||
解决办法:
|
||
|
||
1. 临时解决:
|
||
|
||
```bash
|
||
export LD_LIBRARY_PATH=/opt/gcc-13/lib64:$LD_LIBRARY_PATH
|
||
```
|
||
|
||
2. 编译期内嵌 rpath:
|
||
|
||
```bash
|
||
g++ main.cpp -Wl,-rpath=/opt/gcc-13/lib64 -O2 -o app
|
||
```
|
||
|
||
|
||
---
|
||
|
||
## 七、常见问题与修复要点
|
||
|
||
|问题|原因|解决方案|
|
||
|---|---|---|
|
||
|`configure: error: GMP/MPFR/MPC not found`|系统库过旧或缺失|使用 `contrib/download_prerequisites`|
|
||
|`collect2: error: ld returned 1 exit status`|binutils 太旧|启用 `/opt/binutils-2.40` 并确认 PATH|
|
||
|`checking for sufficient default stack space... no`|栈空间不足|执行 `ulimit -s unlimited`|
|
||
|`cc1plus: out of memory`|并行过高或 swap 不足|降低并行度、添加 swap|
|
||
|
||
---
|
||
|
||
## 八、验证构建质量(可选)
|
||
|
||
自检:
|
||
|
||
```bash
|
||
cd /usr/local/src/build-gcc-13
|
||
sudo make -k check > test.log 2>&1
|
||
grep -A2 "Summary" test.log
|
||
```
|
||
|
||
成功率通常 > 99%,剩余测试多为非致命的浮点容差差异。
|
||
|
||
---
|
||
|
||
## ✅ 总结:完整流程一览
|
||
|
||
```bash
|
||
# 一次性复现构建流程 (示例版)
|
||
sudo dnf groupinstall "Development Tools" -y
|
||
sudo dnf install -y gcc gcc-c++ make bison flex texinfo git wget curl xz gawk perl python3 \
|
||
glibc-devel glibc-headers libstdc++-devel zlib-devel gmp-devel mpfr-devel libmpc-devel isl-devel
|
||
|
||
cd /usr/local/src
|
||
sudo wget https://ftp.gnu.org/gnu/binutils/binutils-2.40.tar.xz
|
||
sudo tar -xf binutils-2.40.tar.xz
|
||
sudo mkdir build-binutils-2.40 && cd build-binutils-2.40
|
||
sudo ../binutils-2.40/configure --prefix=/opt/binutils-2.40 --disable-multilib --enable-gold --enable-ld=default --enable-plugins
|
||
sudo make -j"$(nproc)" && sudo make install
|
||
export PATH=/opt/binutils-2.40/bin:$PATH
|
||
|
||
cd /usr/local/src
|
||
sudo wget https://ftp.gnu.org/gnu/gcc/gcc-13.3.0/gcc-13.3.0.tar.xz
|
||
sudo tar -xf gcc-13.3.0.tar.xz
|
||
cd gcc-13.3.0 && ./contrib/download_prerequisites
|
||
|
||
cd /usr/local/src && sudo mkdir build-gcc-13 && cd build-gcc-13
|
||
sudo ../gcc-13.3.0/configure --prefix=/opt/gcc-13 --enable-languages=c,c++ --disable-multilib \
|
||
--enable-checking=release --enable-lto --enable-threads=posix --with-system-zlib
|
||
sudo make -j"$(nproc)" && sudo make install
|
||
|
||
echo 'export PATH=/opt/gcc-13/bin:$PATH' | sudo tee /etc/profile.d/gcc13.sh
|
||
echo 'export LD_LIBRARY_PATH=/opt/gcc-13/lib64:$LD_LIBRARY_PATH' | sudo tee -a /etc/profile.d/gcc13.sh
|
||
sudo chmod +x /etc/profile.d/gcc13.sh
|
||
source /etc/profile.d/gcc13.sh
|
||
```
|
||
|
||
---
|
||
|
||
是否希望我把上述完整命令整理成 **一键可执行的 build-gcc13.sh 脚本**(带错误检查、日志输出、并自动检测是否已安装 binutils)?
|
||
这样你只需运行一行命令即可完成全部流程。
|
||
|
||
|
||
openssl
|
||
|
||
```
|
||
./Configure --prefix=/usr/local/openssl-3 \
|
||
--openssldir=/usr/local/openssl-3/ssl \
|
||
shared zlib \
|
||
enable-ec_nistp_64_gcc_128 \
|
||
enable-tls1_3
|
||
```
|
||
|
||
```
|
||
make -j$(nproc)
|
||
make install
|
||
```
|
||
|
||
```
|
||
export CC=/usr/local/gcc-13/bin/gcc
|
||
export CXX=/usr/local/gcc-13/bin/g++
|
||
export CPPFLAGS="-I/usr/local/openssl-3/include"
|
||
export LDFLAGS="-L/usr/local/openssl-3/lib64"
|
||
```
|
||
|
||
```
|
||
echo "/usr/local/openssl-3/lib64" | sudo tee /etc/ld.so.conf.d/openssl-3.conf
|
||
```
|
||
|
||
|
||
```
|
||
export CPPFLAGS="-I/usr/local/openssl-3/include"
|
||
export LDFLAGS="-L/usr/local/openssl-3/lib -Wl,-rpath=/usr/local/openssl-3/lib"
|
||
export PKG_CONFIG_PATH="/usr/local/openssl-3/lib/pkgconfig"
|
||
export LD_LIBRARY_PATH="/usr/local/openssl-3/lib:$LD_LIBRARY_PATH"
|
||
export PATH="/usr/local/openssl-3/bin:$PATH"
|
||
|
||
./configure \
|
||
--prefix=/usr/local/openssh-10 \
|
||
--sysconfdir=/usr/local/openssh-10/etc \
|
||
--with-ssl-dir=/usr/local/openssl-3 \
|
||
--with-pam \
|
||
--with-md5-passwords
|
||
|
||
```
|