在 CentOS 上感觉 gcc 编译/运行慢,通常不是 gcc 本身“运行慢”,而是编译过程受多种因素拖累。下面按最常见原因 → 排查方法 → 解决建议来说明。
CentOS 7 默认:
gcc 4.8.5
✅ 解决:
# CentOS 7
yum install centos-release-scl
yum install devtoolset-9-gcc devtoolset-9-gcc-c++
scl enable devtoolset-9 bash
常见错误:
gcc -O0 -g3
-O0:不做任何优化,编译慢、运行也慢-g3:调试信息超大✅ 建议:
gcc -O2 -g
#include <bits/stdc++.h>表现:
✅ 解决:
CentOS 常在:
gcc 会大量读写:
/tmp.o 文件✅ 检查:
df -h
iostat -x 1
✅ 优化:
export TMPDIR=/dev/shm
或把代码放 SSD / tmpfs
gcc 很吃内存(尤其 C++)
free -h
如果 swap 在动,立刻变慢。
✅ 解决:
make -j$(nproc)
# 或
make -j2
CentOS 默认:
会拖慢文件访问。
✅ 排查:
getenforce
systemctl status auditd
❌ 错误:
gcc *.c # 单进程
✅ 正确:
make -j$(nproc)
gcc --version
nproc
free -h
df -h
iostat -x 1
CentOS 上 gcc “慢”,90% 不是 gcc 慢,而是:
- 旧 gcc
- I/O 慢
- 内存不够
- 编译参数不合理
如果你愿意,可以告诉我:
我可以直接给你针对性优化方案。