在 CentOS 上如果 GCC 编译速度慢,可以从 编译器选项、系统资源、构建系统、硬件/环境 几个层面来优化。下面按实用程度从高到低说明。
# 慢
-g3 -O0
# 快
-O2 -g1
-g3 会生成大量调试信息-g-O0(最快编译)-O2(推荐)-O3 -march=native
-O3编译时间明显更长
make -j$(nproc)
或
make -j$(grep -c processor /proc/cpuinfo)
如果是 CMake:
cmake --build . -j$(nproc)
适合 C++ 大项目:
// stdafx.h
#include <vector>
#include <string>
#include <map>
开启:
-gcc -x c++-header stdafx.h
避免重复编译:
yum install ccache
使用:
export CC="ccache gcc"
export CXX="ccache g++"
或
make CC="ccache gcc" CXX="ccache g++"
查看命中率:
ccache -s
mkdir /tmp/build && mount -t tmpfs tmpfs /tmp/build
free -h
如果有 swap 占用:
swapon --show
setenforce 0
或
vi /etc/selinux/config
# SELINUX=disabled
yum install centos-release-scl
yum install devtoolset-11
scl enable devtoolset-11 bash
新 GCC 编译速度通常更好。
yum install ninja-build
cmake -GNinja .
ninja
适合多台机器编译。
make -j1 V=1
top
htop
iostat -x 1
✅ CentOS 编译加速标准方案
ccache + make -j$(nproc) + devtoolset + SSD
如果你愿意,可以告诉我:
我可以给你针对性的优化方案。