在 CentOS 上配置 C++ 运行/编译环境,通常步骤如下(以 CentOS 7 / 8 为例,通用性较强):
sudo yum update -y
sudo yum groupinstall "Development Tools" -y
这会安装:
sudo yum install gcc-c++ -y
g++ --version
gcc --version
mkdir ~/cpp_test
cd ~/cpp_test
创建文件 test.cpp:
#include <iostream>
using namespace std;
int main() {
cout << "Hello CentOS C++" << endl;
return 0;
}
g++ test.cpp -o test
./test
输出:
Hello CentOS C++
sudo yum install cmake -y
编译时加:
g++ test.cpp -o test -lpthread -lm
例如 CentOS 7 默认 gcc 4.8:
sudo yum install centos-release-scl -y
sudo yum install devtoolset-9 -y
scl enable devtoolset-9 bash
或长期使用:
echo "source /opt/rh/devtoolset-9/enable" >> ~/.bashrc
如果是部署程序:
yum install libstdc++ -y
如果你用的是 CentOS Stream / Rocky / Alma / 具体版本,或者需要 VS Code / CLion / Docker C++ 环境,可以告诉我,我可以进一步细化。