温馨提示×

centos怎样安装c++编译器

小樊
38
2026-09-09 00:54:37
栏目: 编程语言

在 CentOS 上安装 C++ 编译器,通常是通过安装 GCC(GNU Compiler Collection) 来实现的。以下是常见做法:


一、使用 yum 安装(推荐,适用于 CentOS 7/8)

1. 更新系统(可选但建议)

sudo yum update

2. 安装 C++ 编译器(g++)

sudo yum install gcc-c++

3. 验证安装

g++ --version

如果看到类似 g++ (GCC) x.x.x 的版本信息,说明安装成功。


二、CentOS 8 / Stream(使用 dnf)

CentOS 8 及以后版本推荐使用 dnf:

sudo dnf install gcc-c++

三、安装完整开发工具组(推荐新手)

如果你想一次性安装 GCC、g++、make 等常用开发工具:

sudo yum groupinstall "Development Tools"

或(CentOS 8+):

sudo dnf groupinstall "Development Tools"

四、测试 C++ 编译

创建测试文件:

vi hello.cpp

内容:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello CentOS C++" << endl;
    return 0;
}

编译并运行:

g++ hello.cpp -o hello
./hello

五、常见问题

1. 没有 yum 权限

使用 sudo 或切换到 root 用户:

su -

2. 需要更高版本 GCC

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),我可以给你更精确的安装方案。

0 踩