Ubuntu下GCC命令个人笔记
1、C编译过程
编译的过程有
1、预编译(Pre-processing)
2、编译(Compiling)
3、汇编(Assembling)
4、链接(Linking)
此语句一步到位,将.c文件生成可执行程序。
gcc main.c -o main
本贴用以下mian.c代码作为示例。
#include <stdio.h>
int mian()
{
printf("hello world!\n");
return 0;
}
2、预编译()
gcc -E main.c -o main.i
我用的是ubuntu12版本,-E选项区分大小写。
此语句预编译产生.i文件,本例中预处理结果就是将stdio.h 文件中的内容插入到test.c中了。
3、编译(compilation)
gcc -S main.i -o main.s
-S表示编译期间,生成汇编代码后停止,并-o生成汇编代码。
4、汇编
gcc -C main.s -o mian.o
-C表示编译期间,将上面的汇编代码生成为目标文件。
5、连接(Linking)
gcc main.o -o main
完成此步后产生可执行的程序main
执行./main即可执行
6、多个源文件的编译
假设有text1.c和text2.c
一步到位
gcc text1.c text2.c -o text
实际上和下面三条语句一样。
gcc -C text1.c -o text1.o
gcc -C text2.c -o text2.o
gcc text1.o text2.o -o text