温馨提示×

centos怎么用gcc编程序

小樊
45
2026-09-17 21:43:08
栏目: 智能运维

在 CentOS 上使用 gcc 编译程序,一般步骤如下:


一、检查 gcc 是否安装

gcc --version

如果提示 command not found,说明还没安装。


二、安装 gcc(CentOS 7 / 8 通用)

CentOS 7

sudo yum install gcc gcc-c++ -y

CentOS 8 / Stream

sudo dnf install gcc gcc-c++ -y

安装完成后再次确认:

gcc --version

三、写一个最简单的 C 程序

创建文件:

vi hello.c

内容:

#include <stdio.h>

int main() {
    printf("Hello CentOS!\n");
    return 0;
}

保存退出(vi 中按 Esc,输入 :wq)。


四、使用 gcc 编译

1️⃣ 基本编译

gcc hello.c

会生成默认可执行文件 a.out

运行:

./a.out

2️⃣ 指定输出文件名

gcc hello.c -o hello

运行:

./hello

五、编译 C++ 程序(可选)

C++ 文件用 g++

g++ test.cpp -o test
./test

六、常用编译参数

gcc hello.c -o hello -Wall    # 显示所有警告
gcc hello.c -o hello -g       # 生成调试信息(gdb用)
gcc hello.c -o hello -O2      # 优化编译

七、多文件编译示例(了解)

gcc main.c func.c -o app

如果你是想:

  • Makefile
  • gcc 编译内核模块
  • CentOS 上配置开发环境

可以告诉我,我可以进一步讲。

0