Debian下使用GCC编译C程序的步骤如下:
安装GCC
执行命令:
sudo apt update
sudo apt install build-essential # 安装GCC及开发工具包
验证安装:
gcc --version
编写C程序
用文本编辑器(如Vim、Nano)创建.c
文件,例如hello.c
:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
保存后退出编辑器。
编译C程序
在终端执行:
gcc -o hello hello.c # -o指定输出文件名,默认生成a.out
若需优化或调试,可添加选项:
-O2
:二级优化(提升性能)-g
:生成调试信息(配合GDB使用)-Wall
:显示所有警告运行程序
执行生成的可执行文件:
./hello
输出结果:Hello, World!
可选步骤:
.c
文件组成,需分别编译后链接,例如:gcc -c main.c -o main.o
gcc -c utils.c -o utils.o
gcc main.o utils.o -o program
make
工具自动化编译流程,适合大型项目。