GCC inline

1、同一文件,默认编译选项

  在同一.c文件内gcc默认编译选项:

    gcc -S test.c -o test1.s

  生成的test.s中不会内联展开,而是对其普通函数处理。

2、同一文件,编译选项O3优化

  在同一.c文件内gcc O3编译选项:

    gcc -S test.c -o test2.s -O3

  生成的test.s中会内联展开

3、不同文件,编译选项O3优化

  gcc -S main.c -o main.s -O3

  gcc -S fun.c -o fun.s -O3

     目测且分析是没有内联展开,具体结果可以看附件。

  分析是gcc编译单独编译文件成汇编代码,而在编译main.c的时候,还没有fun.s的汇编代码,只能做call调用,虽然已经在声明中声明了inline。但是根据https://blog.csdn.net/hanchaoman/article/details/7268839 的表述,声明是不管用的,不给报错就不错了。

附件1-test.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <unistd.h>
#include <fcntl.h>
inline void fun(){
        printf("inline run
");
}
int main(int argc, char *argv[]){
        fun();
        return 0;
}

附件2-test1.s

 1     .file    "test.c"
 2     .section    .rodata
 3 .LC0:
 4     .string    "inline run"
 5     .text
 6     .globl    fun
 7     .type    fun, @function
 8 fun:
 9 .LFB2:
10     .cfi_startproc
11     pushq    %rbp
12     .cfi_def_cfa_offset 16
13     .cfi_offset 6, -16
14     movq    %rsp, %rbp
15     .cfi_def_cfa_register 6
16     movl    $.LC0, %edi
17     call    puts
18     popq    %rbp
19     .cfi_def_cfa 7, 8
20     ret
21     .cfi_endproc
22 .LFE2:
23     .size    fun, .-fun
24     .globl    main
25     .type    main, @function
26 main:
27 .LFB3:
28     .cfi_startproc
29     pushq    %rbp
30     .cfi_def_cfa_offset 16
31     .cfi_offset 6, -16
32     movq    %rsp, %rbp
33     .cfi_def_cfa_register 6
34     subq    $16, %rsp
35     movl    %edi, -4(%rbp)
36     movq    %rsi, -16(%rbp)
37     movl    $0, %eax
38     call    fun
39     movl    $0, %eax
40     leave
41     .cfi_def_cfa 7, 8
42     ret
43     .cfi_endproc
44 .LFE3:
45     .size    main, .-main
46     .ident    "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-39)"
47     .section    .note.GNU-stack,"",@progbits

附件3-test2.s

 1     .file    "test.c"
 2     .section    .rodata.str1.1,"aMS",@progbits,1
 3 .LC0:
 4     .string    "inline run"
 5     .text
 6     .p2align 4,,15
 7     .globl    fun
 8     .type    fun, @function
 9 fun:
10 .LFB35:
11     .cfi_startproc
12     movl    $.LC0, %edi
13     jmp    puts
14     .cfi_endproc
15 .LFE35:
16     .size    fun, .-fun
17     .section    .text.startup,"ax",@progbits
18     .p2align 4,,15
19     .globl    main
20     .type    main, @function
21 main:
22 .LFB36:
23     .cfi_startproc
24     subq    $8, %rsp
25     .cfi_def_cfa_offset 16
26     movl    $.LC0, %edi
27     call    puts
28     xorl    %eax, %eax
29     addq    $8, %rsp
30     .cfi_def_cfa_offset 8
31     ret
32     .cfi_endproc
33 .LFE36:
34     .size    main, .-main
35     .ident    "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-39)"
36     .section    .note.GNU-stack,"",@progbits

附件4-main.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <math.h>
 5 
 6 #include <unistd.h>
 7 #include <fcntl.h>
 8 extern inline void fun();
 9 int main(int argc, char *argv[]){
10         fun();
11         return 0;
12 }

附件5-main.s

 1         .file   "main.c"
 2         .text
 3         .globl  main
 4         .type   main, @function
 5 main:
 6 .LFB2:
 7         .cfi_startproc
 8         pushq   %rbp
 9         .cfi_def_cfa_offset 16
10         .cfi_offset 6, -16 
11         movq    %rsp, %rbp
12         .cfi_def_cfa_register 6
13         subq    $16, %rsp
14         movl    %edi, -4(%rbp)
15         movq    %rsi, -16(%rbp)
16         movl    $0, %eax
17         call    fun 
18         movl    $0, %eax
19         leave
20         .cfi_def_cfa 7, 8
21         ret 
22         .cfi_endproc
23 .LFE2:
24         .size   main, .-main
25         .ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-39)"
26         .section        .note.GNU-stack,"",@progbits

附件6-fun.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <math.h>
 5 
 6 #include <unistd.h>
 7 #include <fcntl.h>
 8 inline void fun(){
 9         printf("inline run
");
10 }

附件7-fun.s

 1         .file   "fun.c"
 2         .section        .rodata.str1.1,"aMS",@progbits,1
 3 .LC0:
 4         .string "inline run"
 5         .text
 6         .p2align 4,,15
 7         .globl  fun 
 8         .type   fun, @function
 9 fun:
10 .LFB35:
11         .cfi_startproc
12         movl    $.LC0, %edi
13         jmp     puts
14         .cfi_endproc
15 .LFE35:
16         .size   fun, .-fun
17         .ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-39)"
18         .section        .note.GNU-stack,"",@progbits
原文地址:https://www.cnblogs.com/micoblog/p/13446110.html