gcc常用命令使用

gcc编译文件过程

  .c文件到 .i文件 到.s(汇编文件)  到.o文件,再到可执行文件

.c到.i

实操一下:

test.c文件如下 :

  1 
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 int main()
  5  {
  6     char buf[8]={0};
  7     int n = snprintf(buf, 8, "%s", "1234567890");
  8     printf("buf: %s
", buf);
  9     printf("n: %d
", n);
 10 
 11  }

gcc -E test.c -o test.i,文件过大,只显示一部份

gcc  -s  test.i

vim test.s

test.s内容如下:

  7 .LC2:
  8         .string "buf: %s
"
  9 .LC3:
 10         .string "n: %d
"
 11         .text
 12         .globl  main
 13         .type   main, @function
 14 main:
 15 .LFB2:
 16         .cfi_startproc
 17         pushq   %rbp
 18         .cfi_def_cfa_offset 16
 19         .cfi_offset 6, -16
 20         movq    %rsp, %rbp
 21         .cfi_def_cfa_register 6
 22         subq    $32, %rsp
 23         movq    %fs:40, %rax
 24         movq    %rax, -8(%rbp)
 25         xorl    %eax, %eax
 26         movq    $0, -16(%rbp)
 27         leaq    -16(%rbp), %rax
 28         movl    $.LC0, %ecx

汇编代码到 .o文件

gcc -c test.s   生成 test.o

生成二进制文件

gcc -c test.o -o test

原文地址:https://www.cnblogs.com/fply/p/8492619.html