在 CentOS 上安装 C++ 编译器,通常是通过安装 GCC(GNU Compiler Collection) 来实现的。以下是常见做法:
sudo yum update
sudo yum install gcc-c++
g++ --version
如果看到类似 g++ (GCC) x.x.x 的版本信息,说明安装成功。
CentOS 8 及以后版本推荐使用 dnf:
sudo dnf install gcc-c++
如果你想一次性安装 GCC、g++、make 等常用开发工具:
sudo yum groupinstall "Development Tools"
或(CentOS 8+):
sudo dnf groupinstall "Development Tools"
创建测试文件:
vi hello.cpp
内容:
#include <iostream>
using namespace std;
int main() {
cout << "Hello CentOS C++" << endl;
return 0;
}
编译并运行:
g++ hello.cpp -o hello
./hello
使用 sudo 或切换到 root 用户:
su -
CentOS 自带 GCC 版本较旧,可安装 Software Collections (SCL) 或使用 DevToolset:
sudo yum install centos-release-scl
sudo yum install devtoolset-11
scl enable devtoolset-11 bash
如果你告诉我 CentOS 版本号(如 7、8、Stream),我可以给你更精确的安装方案。