Linux编译C程序

  • 原创
  • Madman
  • /
  • /
  • 0
  • 9936 次阅读

Linux编译C程序-min.png

Synopsis: 分析了Linux环境下从创建C语言源代码到最终的可执行文件的整个过程,中间经过于处理器的预处理阶段(源文件到预处理文件),编译器的编译阶段(预处理文件到汇编文件),汇编器的汇编阶段(汇编文件到可重定向的目标文件),链接器的链接阶段(可重定向的目标文件到可执行的目标文件)。另外简单地介绍了gdb调试工具

1. 创建源代码

可以使用vimnano工具编辑源代码文件:

[root@CentOS ~]# vim hello.c

内容如下:
#include <stdio.h>
int main(){
    printf("Hello, World!\n");
    return 0;
}

vim一般模式下(按ESC退出编辑模式),输入:%!xxd可以查看hello.c的16进制形式,如果要回到文本格式则输入:%!xxd -r。如果输入:%!xxd -b可以查看hello.c的二进制形式,这是hello.c保存在磁盘上的存储状态。

2. 编译与运行

[root@CentOS ~]# yum -y install gcc

gcc命令的基本用法为gcc [options] [filenames],如果只指定源代码文件名而不使用任何选项时,gcc将会自动编译产生一个a.out的可执行文件:

[root@CentOS ~]# gcc hello.c
[root@CentOS ~]# ls -l
-rw-r--r--  1 root root        78 Jan 13 09:45 hello.c
-rwxr-xr-x  1 root root     34749 Jan 13 09:41 a.out
[root@CentOS ~]# ./a.out
Hello, World!

使用-o选项可以指定编译后生成的文件名:

[root@CentOS ~]# gcc -o hello hello.c
[root@CentOS ~]# ls -l
-rw-r--r--  1 root root        78 Jan 13 09:45 hello.c
-rwxr-xr-x  1 root root      6425 Jan 13 09:46 hello
[root@CentOS ~]# ./hello
Hello, World!

3. 分析过程

c编译过程解析.png

3.1 预处理阶段

使用-E选项只做预处理不编译,当使用这个选项时,预处理器的输出被送到标准输出而不是存储到文件。如果想将预处理的输出存储到文件,可结合-o选项使用,使用如下:

[root@CentOS ~]# gcc -E hello.c -o hello.i
[root@CentOS ~]# ls -l
-rw-r--r--  1 root root        78 Jan 13 09:45 hello.c
-rw-r--r--  1 root root     16738 Jan 13 10:06 hello.i
[root@CentOS ~]# less hello.i

                                
                            
分类: Linux
标签: 编译 gcc gdb 源代码
  • Right Away Reach Your Funds idurhgdkgm.temp.swtest.ru E8
  • Apply Your Premier Bonus Offer cz092227.tw1.ru Mj
  • Reach Your Level-Based Award cz092227.tw1.ru 3c
  • Earn Your Premium Bonus cu594050.tw1.ru WR
  • Unlock Your Reach Cash cz092227.tw1.ru jd
  • Enter to Get Your Amazing Gift cz092227.tw1.ru MR
  • Get Your Casino Reward idurhgdkgm.temp.swtest.ru 6A
  • Delight in Your Arcade-Game Extra idurhgdkgm.temp.swtest.ru KG
  • Appreciate Your Superior Blessed Funds cz092227.tw1.ru rr
  • Open Your Baccarat Funds cu594050.tw1.ru QP
  • Enable Your Ultimate Extra Credit idurhgdkgm.temp.swtest.ru pO
  • Secure Your Big Stake Bonus cz092227.tw1.ru Iy
  • Secure Your Money Instantly cu594050.tw1.ru Lj
  • Open Your Comfortable Plays idurhgdkgm.temp.swtest.ru Jk
  • Enjoy Your Demonstrated Reward cz092227.tw1.ru KC
  • Get Your Boon Reward idurhgdkgm.temp.swtest.ru fk
  • Open Your Digital Money Prize cu594050.tw1.ru 6S
  • Get Your Outstanding Cash cu594050.tw1.ru H8
  • Activate Your Web Token Reward idurhgdkgm.temp.swtest.ru dv
  • Redeem Your Supreme Cash cu594050.tw1.ru CF
  • Start Your Digital Token Prize cz092227.tw1.ru lp
  • Earn Your Chance Reward cz092227.tw1.ru lO
  • Gather Your Victory Extra cu594050.tw1.ru EF
  • Gather Your Higher-Quality Money idurhgdkgm.temp.swtest.ru E1
未经允许不得转载: LIFE & SHARE - 王颜公子 » Linux编译C程序

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列