在 CentOS 无外网(离线)环境 下配置 C++ 开发环境,一般有三种常见方案,按推荐程度给你说明。
找一台 同版本 CentOS(如 CentOS 7)的机器:
mkdir cpp-offline
cd cpp-offline
yum install --downloadonly --downloaddir=. gcc gcc-c++ make
会下载:
gccgcc-c++glibc-devellibstdc++-develmake用 U 盘 / 内网传输:
scp -r cpp-offline user@离线机器:/opt/
cd /opt/cpp-offline
rpm -Uvh *.rpm --nodeps
或(推荐):
yum localinstall *.rpm
gcc -v
g++ -v
mount -o loop CentOS-7-x86_64-DVD.iso /mnt
cat > /etc/yum.repos.d/local.repo <<EOF
[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0
EOF
yum install gcc gcc-c++ make
✅ 无需外网
仅在无法用 RPM / ISO 时使用:
wget gcc-*.tar.gz
tar -xzf gcc-*.tar.gz
cd gcc-*
./contrib/download_prerequisites
mkdir build && cd build
../configure --disable-multilib
make -j4
make install
⚠️ 依赖极多,极易失败
// test.cpp
#include <iostream>
int main(){
std::cout << "Hello C++" << std::endl;
}
g++ test.cpp -o test
./test
✅ 版本必须一致(CentOS 7 ≠ 8)
✅ 优先用 gcc-c++ 而不是只装 gcc
✅ 缺 make 会编译不了
如果你告诉我:
我可以给你 精确命令包清单。