首页 > 编程知识 正文

linux编写c语言程序,linux怎么用gcc编译

时间:2023-05-05 11:26:21 阅读:34745 作者:1338

编译进程预处理的编译程序集的链接执行

gcc编译c语言程序的过程分为四个步骤:

1 .预处理

-使用e命令生成.i文件。 2 .编译

-S命令将.i文件转换为.s文件。 3 .汇编

使用-c命令生成指向.s文件.o文件4 .的链接

此外,使用-o命令链接每个文件以生成可执行文件。 无后缀)。

-o命令将输出文件输出到指定的文件

以test.c为例,输出hello world。

使用cat命令显示文件的内容。 -n表示显示函数。

cat -n test.c的结果:

1#includestdio.H2intmain(3)4printf ) ' Helloworld! n '; 输出赫尔洛魔兽! 5返回0; 6 )对于常规编译,一步就可以编译链接,生成并运行可执行文件。方法1

gcc test.c -o test ./test上的编译链接中的test.c和-o test可以重新定位,结果相同。

方法2

gcc test.c./a.out gcc test.i或gcc test.o或gcc test.s生成默认的可执行文件a.out

预处理为编译进行准备工作,替换源程序中包含#的代码。

例如,源程序中的#include stdio.h命令指示预处理器读取系统头文件stdio.h中的内容,并将其直接插入到程序文本中。 (注意,头文件的内容只是声明,没有相关代码实现,实现代码是在链接阶段获取的。)

输入命令:

如果不附加GCC-etest.c-otest.I-otest.I,则直接进行预处理并显示结果,不会生成.i文件。

结果如下。

1 #1' test.c '2#1' built-in '3#1' command-line '4# 31 ' command-line '5#1' include/x86 _ 64-Linux-g inux include/x86 _ 64-Linux-GNU/bits/libc-header-start.h ' 3412 #1'/usr/iii cdefs.h ' 13415 # 452 '/usr/include/x86 _ 64-Linux-GNU/sys/cdefs.64 wordsize.h ' 13 (4) 17 ) 453 )/usr x86 ) ) ) ) ) usr/include/x86 _ 64-Linux-GNU/sys/cdefs.h ' 23420 # 462 '/usr/include/features.h ' . 719 externvoidfunlockfile (file * _ _ stream ) _Attribute__((__nothrow__,__leaf__ ) ) ) 722扩展int _ _ overflow (file *,int ); 723 # 873 '/usr/include/stdio.h ' 34724725 #2' test.c ' 2726727 #2' test.c ' 728 int main (

730 printf("hello world!n"); 731 return 0; 732} 编译

将源程序语言转换成汇编语言,生成 . o 文件。
输入命令:

gcc -S test.i -o test.s

此处的 -o test.s 可以去掉,不加也会自动生成test.s文件
结果:

1.file"test.c" 2.text 3.section.rodata 4.LC0: 5.string"hello world!" 6.text 7.globlmain 8.typemain, @function 9main: 10.LFB0: 11.cfi_startproc 12endbr64 13pushq%rbp 14.cfi_def_cfa_offset 16 15.cfi_offset 6, -16 16movq%rsp, %rbp 17.cfi_def_cfa_register 6 18leaq.LC0(%rip), %rdi 19callputs@PLT 20movl$0, %eax 21popq%rbp 22.cfi_def_cfa 7, 8 23ret 24.cfi_endproc 25.LFE0: 26.sizemain, .-main 27.ident"GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0" 28.section.note.GNU-stack,"",@progbits 29.section.note.gnu.property,"a" 30.align 8 31.long 1f - 0f 32.long 4f - 1f 33.long 5 340: 35.string "GNU" 361: 37.align 8 38.long 0xc0000002 39.long 3f - 2f 402: 41.long 0x3 423: 43.align 8 444: 汇编

将汇编语言转换成机器语言,生成 . s 文件。

gcc -c test.s -o test.o

此处的 -o test.o可以去掉,不加也会自动生成test.o文件

此处用cat打开test.o可能回乱码,所以使用以下命令:

hexdump test.o

结果:

0000000 457f 464c 0102 0001 0000 0000 0000 00000000010 0001 003e 0001 0000 0000 0000 0000 00000000020 0000 0000 0000 0000 0310 0000 0000 00000000030 0000 0000 0040 0000 0000 0040 000e 000d0000040 0ff3 fa1e 4855 e589 8d48 003d 0000 e8000000050 0000 0000 00b8 0000 5d00 68c3 6c65 6f6c0000060 7720 726f 646c 0021 4700 4343 203a 55280000070 7562 746e 2075 2e39 2e33 2d30 3731 62750000080 6e75 7574 7e31 3032 302e 2934 3920 332e0000090 302e 0000 0000 0000 0004 0000 0010 000000000a0 0005 0000 4e47 0055 0002 c000 0004 000000000b0 0003 0000 0000 0000 0014 0000 0000 000000000c0 7a01 0052 7801 0110 0c1b 0807 0190 000000000d0 001c 0000 001c 0000 0000 0000 001b 000000000e0 4500 100e 0286 0d43 5206 070c 0008 000000000f0 0000 0000 0000 0000 0000 0000 0000 00000000100 0000 0000 0000 0000 0001 0000 0004 fff10000110 0000 0000 0000 0000 0000 0000 0000 00000000120 0000 0000 0003 0001 0000 0000 0000 00000000130 0000 0000 0000 0000 0000 0000 0003 0003........

无论是windows还是linux,为了方便人类观察,计算机上的二进制数都用十六进制来表示,生成的都是十六进制代码,转化成二进制很方便,所以就等价于生成机器代码。

链接

这个阶段将有关的目标文件彼此相连接,也就是把可执行程序需要的所有的编译过程产生的.o文件组合到一起。比如gcc会在lib文件(实际上就算打包的.o文件集合)寻找函数实现的定义。
输入命令:

gcc test.o -o test 运行

有两种可执行文件,一种是默认生成的a.out(历史遗留原因),一种无后缀的可执行文件,运行哪个结果都一样。
输入命令:

./test.c

结果:

hello world!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。