c语言怎么编译(详细示例)


当前第2页 返回上一页

上述命令中-S让编译器在编译之后停止,不进行后续过程。编译过程完成后,将生成程序的汇编代码test.s,这也是文本文件,内容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

// test.c汇编之后的结果test.s

    .file   "test.c"

    .section    .rodata

.LC0:

    .string "a=%d, b=%d, a+b=%d\n"

    .text

    .globl  main

    .type   main, @function

main:

.LFB0:

    .cfi_startproc

    pushl   %ebp

    .cfi_def_cfa_offset 8

    .cfi_offset 5, -8

    movl    %esp, %ebp

    .cfi_def_cfa_register 5

    andl    $-16, %esp

    subl    $32, %esp

    movl    $2, 20(%esp)

    movl    $3, 24(%esp)

    movl    24(%esp), %eax

    movl    %eax, 4(%esp)

    movl    20(%esp), %eax

    movl    %eax, (%esp)

    call    add

    movl    %eax, 28(%esp)

    movl    28(%esp), %eax

    movl    %eax, 12(%esp)

    movl    24(%esp), %eax

    movl    %eax, 8(%esp)

    movl    20(%esp), %eax

    movl    %eax, 4(%esp)

    movl    $.LC0, (%esp)

    call    printf

    leave

    .cfi_restore 5

    .cfi_def_cfa 4, 4

    ret

    .cfi_endproc

.LFE0:

    .size   main, .-main

    .ident  "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"

    .section    .note.GNU-stack,"",@progbits

3.汇编(Assemble)

汇编过程将上一步的汇编代码转换成机器码(machine code),这一步产生的文件叫做目标文件,是二进制格式。gcc汇编过程通过as命令完成:

$ as test.s -o test.o

等价于:

gcc -c test.s -o test.o

这一步会为每一个源文件产生一个目标文件。因此mymath.c也需要产生一个mymath.o文件

4.链接(Linking)

链接过程将多个目标文以及所需的库文件(.so等)链接成最终的可执行文件(executable file)。

命令大致如下:

$ ld -o test.out test.o inc/mymath.o ...libraries...

结语

经过以上分析,我们发现编译过程并不像想象的那么简单,而是要经过预处理、编译、汇编、链接。尽管我们平时使用gcc命令的时候没有关心中间结果,但每次程序的编译都少不了这几个步骤。也不用为上述繁琐过程而烦恼,因为你仍然可以:

$ gcc hello.c # 编译

$ ./a.out # 执行

以上就是c语言怎么编译(详细示例)的详细内容!

返回前面的内容

相关阅读 >>

C语言字符常量的合法表示形式是什么

C语言取余符号是什么

C语言自定义函数(图文详解)

C语言的代码是什么?

C语言在gcc中怎么运行程序?

int占几个字节(C语言)?

C语言自定义函数

C语言自学难度高么?

C语言合法标识符的要求是什么

C语言函数由哪两部分组成

更多相关阅读请进入《C语言》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...