您现在的位置: 主页 > 嵌入式操作系统 > Linux > Linux下编译C文件的过程
本文所属标签:
为本文创立个标签吧:

Linux下编译C文件的过程

来源:fromnet 网络用户发布,如有版权联系网管删除 2018-08-08 

 

1、首先,写一个简单的C源文件:HelloWorld

 

#include <stdio.h>int main(){        printf("/nHello World!/n/n");        return 0;}

 

 

2、下面是调用gcc命令编译HelloWorld.c的过程:

[root@localhost gcc_test]# ls -l
total 4
-rw-r--r-- 1 root root 78 Sep 11 00:17 HelloWorld.c

 

// gcc的-E选项,可以让编译器在预处理后停止,并输出预处理结果。
[root@localhost gcc_test]# gcc -E HelloWorld.c -o step1.i


[root@localhost gcc_test]# ls -l
total 24
-rw-r--r-- 1 root root    78 Sep 11 00:17 HelloWorld.c
-rw-r--r-- 1 root root 18020 Sep 11 00:22 step1.i

 

// gcc的-S选项,表示在生成汇编代码后停止,-o输出汇编代码文件。
[root@localhost gcc_test]# gcc -S step1.i -o step2.s


[root@localhost gcc_test]# ls -l
total 28
-rw-r--r-- 1 root root    78 Sep 11 00:17 HelloWorld.c
-rw-r--r-- 1 root root 18020 Sep 11 00:22 step1.i
-rw-r--r-- 1 root root   454 Sep 11 00:22 step2.s

 

// gas汇编器负责将生成的汇编代码文件编译为目标文件
[root@localhost gcc_test]# gcc -c step2.s -o step3.o


[root@localhost gcc_test]# ls -l
total 32
-rw-r--r-- 1 root root    78 Sep 11 00:17 HelloWorld.c
-rw-r--r-- 1 root root 18020 Sep 11 00:22 step1.i
-rw-r--r-- 1 root root   454 Sep 11 00:22 step2.s
-rw-r--r-- 1 root root   880 Sep 11 00:23 step3.o

 

// gcc连接器,负责将程序的目标文件与所需的所有附加的目标文件连接起来,最终生成可执行文件。附加的目标文件包括静态连接库和动态连接库。
[root@localhost gcc_test]# gcc step3.o -o step4_result


[root@localhost gcc_test]# ls -l
total 40
-rw-r--r-- 1 root root    78 Sep 11 00:17 HelloWorld.c
-rw-r--r-- 1 root root 18020 Sep 11 00:22 step1.i
-rw-r--r-- 1 root root   454 Sep 11 00:22 step2.s
-rw-r--r-- 1 root root   880 Sep 11 00:23 step3.o
-rwxr-xr-x 1 root root  4734 Sep 11 00:23 step4_result

 

// 执行可执行程序
[root@localhost gcc_test]# ./step4_result 


Hello World!


[root@localhost gcc_test]# 
 

 

3、上述拆分的过程完全可以一步执行:

[root@localhost gcc_test]# gcc HelloWorld.c -o result
[root@localhost gcc_test]# ls -l
total 48
-rw-r--r-- 1 root root    78 Sep 11 00:17 HelloWorld.c
-rwxr-xr-x 1 root root  4734 Sep 11 00:39 result
-rw-r--r-- 1 root root 18020 Sep 11 00:22 step1.i
-rw-r--r-- 1 root root   454 Sep 11 00:22 step2.s
-rw-r--r-- 1 root root   880 Sep 11 00:23 step3.o
-rwxr-xr-x 1 root root  4734 Sep 11 00:23 step4_result
[root@localhost gcc_test]# ./result 


Hello World!


[root@localhost gcc_test]# 



              查看评论 回复



嵌入式交流网主页 > 嵌入式操作系统 > Linux > Linux下编译C文件的过程
 文件 ------ 汇编

"Linux下编译C文件的过程"的相关文章

网站地图

围观()