码迷,mamicode.com
首页 > 其他好文 > 详细

gcc编译的过程

时间:2020-01-31 19:14:53      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:stat   put   实践   保存文件   tde   type   pid_t   end   error   

今天再次看了一下Linux下gcc编译的内部工作原理,决定写个博客加深一下自己的印象,如有错误欢迎大家指正。参考书籍《linux c与c++一线开发实践》。

gcc对c/c++语言的编译过程分为四个阶段:预处理、编译、汇编、链接。

1,预处理:是对源程序中的伪指令、特殊符号进行处理的过程。(展开所有宏定义、处理条件编译命令和预编译指令、删除注释等)

首先编写一个代码,保存文件为test.c

 1 #include<stdio.h>
 2  
 3  int main(int argc,char **argv[])
 4  {
 5      char sz[] = "Hello world!\n";
 6      printf("%s",sz);
 7      fflush(stdout);
 8  
 9      return 0;                                                        
10  
11  }

其中函数fflush(stdout)的功能为:将缓冲区的内容输出到屏幕上。

-E对test.c文件进行预处理命令,并保存为test.i文件,如下:

gcc -E test.c -o test.i

使用cat -n test.i命令查看test.i所包含的内容(其中-n是为了显示行号)

   1  # 1 "test.c"
     2  # 1 "<built-in>"
     3  # 1 "<命令行>"
     4  # 31 "<命令行>"
     5  # 1 "/usr/include/stdc-predef.h" 1 3 4
     6  # 32 "<命令行>" 2
     7  # 1 "test.c"
     8  # 12 "test.c"
     9  # 1 "/usr/include/stdio.h" 1 3 4
    10  # 27 "/usr/include/stdio.h" 3 4
    11  # 1 "/usr/include/bits/libc-header-start.h" 1 3 4
    12  # 33 "/usr/include/bits/libc-header-start.h" 3 4
    13  # 1 "/usr/include/features.h" 1 3 4
    14  # 450 "/usr/include/features.h" 3 4
    15  # 1 "/usr/include/sys/cdefs.h" 1 3 4
    16  # 460 "/usr/include/sys/cdefs.h" 3 4
    17  # 1 "/usr/include/bits/wordsize.h" 1 3 4
    18  # 461 "/usr/include/sys/cdefs.h" 2 3 4
    19  # 1 "/usr/include/bits/long-double.h" 1 3 4
    20  # 462 "/usr/include/sys/cdefs.h" 2 3 4
    21  # 451 "/usr/include/features.h" 2 3 4
    22  # 474 "/usr/include/features.h" 3 4
    23  # 1 "/usr/include/gnu/stubs.h" 1 3 4
    24  # 10 "/usr/include/gnu/stubs.h" 3 4
    25  # 1 "/usr/include/gnu/stubs-64.h" 1 3 4
    26  # 11 "/usr/include/gnu/stubs.h" 2 3 4
    27  # 475 "/usr/include/features.h" 2 3 4
    28  # 34 "/usr/include/bits/libc-header-start.h" 2 3 4
    29  # 28 "/usr/include/stdio.h" 2 3 4
    30
    31
    32
    33
    34
    35  # 1 "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/stddef.h" 1 3 4
    36  # 209 "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/stddef.h" 3 4
    37
    38  # 209 "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/stddef.h" 3 4
    39  typedef long unsigned int size_t;
    40  # 34 "/usr/include/stdio.h" 2 3 4
    41
    42
    43  # 1 "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/stdarg.h" 1 3 4
    44  # 40 "/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/stdarg.h" 3 4
    45  typedef __builtin_va_list __gnuc_va_list;
    46  # 37 "/usr/include/stdio.h" 2 3 4
    47
    48  # 1 "/usr/include/bits/types.h" 1 3 4
    49  # 27 "/usr/include/bits/types.h" 3 4
    50  # 1 "/usr/include/bits/wordsize.h" 1 3 4
    51  # 28 "/usr/include/bits/types.h" 2 3 4
    52  # 1 "/usr/include/bits/timesize.h" 1 3 4
    53  # 29 "/usr/include/bits/types.h" 2 3 4
    54
    55
    56  typedef unsigned char __u_char;
    57  typedef unsigned short int __u_short;
    58  typedef unsigned int __u_int;
    59  typedef unsigned long int __u_long;
    60
    61
    62  typedef signed char __int8_t;
    63  typedef unsigned char __uint8_t;
    64  typedef signed short int __int16_t;
    65  typedef unsigned short int __uint16_t;
    66  typedef signed int __int32_t;
    67  typedef unsigned int __uint32_t;
    68
    69  typedef signed long int __int64_t;
    70  typedef unsigned long int __uint64_t;
    71
    72
    73
    74
    75
    76
    77  typedef __int8_t __int_least8_t;
    78  typedef __uint8_t __uint_least8_t;
    79  typedef __int16_t __int_least16_t;
    80  typedef __uint16_t __uint_least16_t;
    81  typedef __int32_t __int_least32_t;
    82  typedef __uint32_t __uint_least32_t;
    83  typedef __int64_t __int_least64_t;
    84  typedef __uint64_t __uint_least64_t;
    85
    86
    87
    88  typedef long int __quad_t;
    89  typedef unsigned long int __u_quad_t;
    90
    91
    92
    93
    94
    95
    96
    97  typedef long int __intmax_t;
    98  typedef unsigned long int __uintmax_t;
    99  # 141 "/usr/include/bits/types.h" 3 4
   100  # 1 "/usr/include/bits/typesizes.h" 1 3 4
   101  # 142 "/usr/include/bits/types.h" 2 3 4
   102  # 1 "/usr/include/bits/time64.h" 1 3 4
   103  # 143 "/usr/include/bits/types.h" 2 3 4
   104
   105
   106  typedef unsigned long int __dev_t;
   107  typedef unsigned int __uid_t;
   108  typedef unsigned int __gid_t;
   109  typedef unsigned long int __ino_t;
   110  typedef unsigned long int __ino64_t;
   111  typedef unsigned int __mode_t;
   112  typedef unsigned long int __nlink_t;
   113  typedef long int __off_t;
   114  typedef long int __off64_t;
   115  typedef int __pid_t;
   116  typedef struct { int __val[2]; } __fsid_t;
   117  typedef long int __clock_t;
   118  typedef unsigned long int __rlim_t;
   119  typedef unsigned long int __rlim64_t;
   120  typedef unsigned int __id_t;
   121  typedef long int __time_t;
   122  typedef unsigned int __useconds_t;
   123  typedef long int __suseconds_t;
   124
   125  typedef int __daddr_t;
   126  typedef int __key_t;
   127
   128
   129  typedef int __clockid_t;
   130
   131
   132  typedef void * __timer_t;
   133
   134
   135  typedef long int __blksize_t;
   136
   137
   138
   139
   140  typedef long int __blkcnt_t;
   141  typedef long int __blkcnt64_t;
   142
   143
   144  typedef unsigned long int __fsblkcnt_t;
   145  typedef unsigned long int __fsblkcnt64_t;
   146
   147
   148  typedef unsigned long int __fsfilcnt_t;
   149  typedef unsigned long int __fsfilcnt64_t;
   150
   151
   152  typedef long int __fsword_t;
   153
   154  typedef long int __ssize_t;
   155
   156
   157  typedef long int __syscall_slong_t;
   158
   159  typedef unsigned long int __syscall_ulong_t;
   160
   161
   162
   163  typedef __off64_t __loff_t;
   164  typedef char *__caddr_t;
   165
   166
   167  typedef long int __intptr_t;
   168
   169
   170  typedef unsigned int __socklen_t;
   171
   172
   173
   174
   175  typedef int __sig_atomic_t;
   176  # 39 "/usr/include/stdio.h" 2 3 4
   177  # 1 "/usr/include/bits/types/__fpos_t.h" 1 3 4
   178
   179
   180
   181
   182  # 1 "/usr/include/bits/types/__mbstate_t.h" 1 3 4
   183  # 13 "/usr/include/bits/types/__mbstate_t.h" 3 4
   184  typedef struct
   185  {
   186    int __count;
   187    union
   188    {
   189      unsigned int __wch;
   190      char __wchb[4];
   191    } __value;
   192  } __mbstate_t;
   193  # 6 "/usr/include/bits/types/__fpos_t.h" 2 3 4
   194
   195
   196
   197
   198  typedef struct _G_fpos_t
   199  {
   200    __off_t __pos;
   201    __mbstate_t __state;
   202  } __fpos_t;
   203  # 40 "/usr/include/stdio.h" 2 3 4
   204  # 1 "/usr/include/bits/types/__fpos64_t.h" 1 3 4
   205  # 10 "/usr/include/bits/types/__fpos64_t.h" 3 4
   206  typedef struct _G_fpos64_t
   207  {
   208    __off64_t __pos;
   209    __mbstate_t __state;
   210  } __fpos64_t;
   211  # 41 "/usr/include/stdio.h" 2 3 4
   212  # 1 "/usr/include/bits/types/__FILE.h" 1 3 4
   213
   214
   215
   216  struct _IO_FILE;
   217  typedef struct _IO_FILE __FILE;
   218  # 42 "/usr/include/stdio.h" 2 3 4
   219  # 1 "/usr/include/bits/types/FILE.h" 1 3 4
   220
   221
   222
   223  struct _IO_FILE;
   224
   225
   226  typedef struct _IO_FILE FILE;
   227  # 43 "/usr/include/stdio.h" 2 3 4
   228  # 1 "/usr/include/bits/types/struct_FILE.h" 1 3 4
   229  # 35 "/usr/include/bits/types/struct_FILE.h" 3 4
   230  struct _IO_FILE;
   231  struct _IO_marker;
   232  struct _IO_codecvt;
   233  struct _IO_wide_data;
   234
   235
   236
   237
   238  typedef void _IO_lock_t;
   239
   240
   241
   242
   243
   244  struct _IO_FILE
   245  {
   246    int _flags;
   247
   248
   249    char *_IO_read_ptr;
   250    char *_IO_read_end;
   251    char *_IO_read_base;
   252    char *_IO_write_base;
   253    char *_IO_write_ptr;
   254    char *_IO_write_end;
   255    char *_IO_buf_base;
   256    char *_IO_buf_end;
   257
   258
   259    char *_IO_save_base;
   260    char *_IO_backup_base;
   261    char *_IO_save_end;
   262
   263    struct _IO_marker *_markers;
   264
   265    struct _IO_FILE *_chain;
   266
   267    int _fileno;
   268    int _flags2;
   269    __off_t _old_offset;
   270
   271
   272    unsigned short _cur_column;
   273    signed char _vtable_offset;
   274    char _shortbuf[1];
   275
   276    _IO_lock_t *_lock;
   277
   278
   279
   280
   281
   282
   283
   284    __off64_t _offset;
   285
   286    struct _IO_codecvt *_codecvt;
   287    struct _IO_wide_data *_wide_data;
   288    struct _IO_FILE *_freeres_list;
   289    void *_freeres_buf;
   290    size_t __pad5;
   291    int _mode;
   292
   293    char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
   294  };
   295  # 44 "/usr/include/stdio.h" 2 3 4
   296  # 52 "/usr/include/stdio.h" 3 4
   297  typedef __gnuc_va_list va_list;
   298  # 63 "/usr/include/stdio.h" 3 4
   299  typedef __off_t off_t;
   300  # 77 "/usr/include/stdio.h" 3 4
   301  typedef __ssize_t ssize_t;
   302
   303
   304
   305
   306
   307
   308  typedef __fpos_t fpos_t;
   309  # 133 "/usr/include/stdio.h" 3 4
   310  # 1 "/usr/include/bits/stdio_lim.h" 1 3 4
   311  # 134 "/usr/include/stdio.h" 2 3 4
   312
   313
   314
   315  extern FILE *stdin;
   316  extern FILE *stdout;
   317  extern FILE *stderr;
   318
   319
   320
   321
   322
   323
   324  extern int remove (const char *__filename) __attribute__ ((__nothrow__ , __leaf__));
   325
   326  extern int rename (const char *__old, const char *__new) __attribute__ ((__nothrow__ , __leaf__));
   327
   328
   329
   330  extern int renameat (int __oldfd, const char *__old, int __newfd,
   331         const char *__new) __attribute__ ((__nothrow__ , __leaf__));
   332  # 173 "/usr/include/stdio.h" 3 4
   333  extern FILE *tmpfile (void) ;
   334  # 187 "/usr/include/stdio.h" 3 4
   335  extern char *tmpnam (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ;
   336
   337
   338
   339
   340  extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ;
   341  # 204 "/usr/include/stdio.h" 3 4
   342  extern char *tempnam (const char *__dir, const char *__pfx)
   343       __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ;
   344
   345
   346
   347
   348
   349
   350
   351  extern int fclose (FILE *__stream);
   352
   353
   354
   355
   356  extern int fflush (FILE *__stream);
   357  # 227 "/usr/include/stdio.h" 3 4
   358  extern int fflush_unlocked (FILE *__stream);
   359  # 246 "/usr/include/stdio.h" 3 4
   360  extern FILE *fopen (const char *__restrict __filename,
   361        const char *__restrict __modes) ;
   362
   363
   364
   365
   366  extern FILE *freopen (const char *__restrict __filename,
   367          const char *__restrict __modes,
   368          FILE *__restrict __stream) ;
   369  # 279 "/usr/include/stdio.h" 3 4
   370  extern FILE *fdopen (int __fd, const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ;
   371  # 292 "/usr/include/stdio.h" 3 4
   372  extern FILE *fmemopen (void *__s, size_t __len, const char *__modes)
   373    __attribute__ ((__nothrow__ , __leaf__)) ;
   374
   375
   376
   377
   378  extern FILE *open_memstream (char **__bufloc, size_t *__sizeloc) __attribute__ ((__nothrow__ , __leaf__)) ;
   379
   380
   381
   382
   383
   384  extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__ , __leaf__));
   385
   386
   387
   388  extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf,
   389        int __modes, size_t __n) __attribute__ ((__nothrow__ , __leaf__));
   390
   391
   392
   393
   394  extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf,
   395           size_t __size) __attribute__ ((__nothrow__ , __leaf__));
   396
   397
   398  extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
   399
   400
   401
   402
   403
   404
   405
   406  extern int fprintf (FILE *__restrict __stream,
   407        const char *__restrict __format, ...);
   408
   409
   410
   411
   412  extern int printf (const char *__restrict __format, ...);
   413
   414  extern int sprintf (char *__restrict __s,
   415        const char *__restrict __format, ...) __attribute__ ((__nothrow__));
   416
   417
   418
   419
   420
   421  extern int vfprintf (FILE *__restrict __s, const char *__restrict __format,
   422         __gnuc_va_list __arg);
   423
   424
   425
   426
   427  extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg);
   428
   429  extern int vsprintf (char *__restrict __s, const char *__restrict __format,
   430         __gnuc_va_list __arg) __attribute__ ((__nothrow__));
   431
   432
   433
   434  extern int snprintf (char *__restrict __s, size_t __maxlen,
   435         const char *__restrict __format, ...)
   436       __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4)));
   437
   438  extern int vsnprintf (char *__restrict __s, size_t __maxlen,
   439          const char *__restrict __format, __gnuc_va_list __arg)
   440       __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0)));
   441  # 379 "/usr/include/stdio.h" 3 4
   442  extern int vdprintf (int __fd, const char *__restrict __fmt,
   443         __gnuc_va_list __arg)
   444       __attribute__ ((__format__ (__printf__, 2, 0)));
   445  extern int dprintf (int __fd, const char *__restrict __fmt, ...)
   446       __attribute__ ((__format__ (__printf__, 2, 3)));
   447
   448
   449
   450
   451
   452
   453
   454  extern int fscanf (FILE *__restrict __stream,
   455       const char *__restrict __format, ...) ;
   456
   457
   458
   459
   460  extern int scanf (const char *__restrict __format, ...) ;
   461
   462  extern int sscanf (const char *__restrict __s,
   463       const char *__restrict __format, ...) __attribute__ ((__nothrow__ , __leaf__));
   464
   465
   466
   467
   468
   469
   470  extern int fscanf (FILE *__restrict __stream, const char *__restrict __format, ...) __asm__ ("" "__isoc99_fscanf")
   471
   472                                 ;
   473  extern int scanf (const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf")
   474                                ;
   475  extern int sscanf (const char *__restrict __s, const char *__restrict __format, ...) __asm__ ("" "__isoc99_sscanf") __attribute__ ((__nothrow__ , __leaf__))
   476
   477                        ;
   478  # 432 "/usr/include/stdio.h" 3 4
   479  extern int vfscanf (FILE *__restrict __s, const char *__restrict __format,
   480        __gnuc_va_list __arg)
   481       __attribute__ ((__format__ (__scanf__, 2, 0))) ;
   482
   483
   484
   485
   486
   487  extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg)
   488       __attribute__ ((__format__ (__scanf__, 1, 0))) ;
   489
   490
   491  extern int vsscanf (const char *__restrict __s,
   492        const char *__restrict __format, __gnuc_va_list __arg)
   493       __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__format__ (__scanf__, 2, 0)));
   494
   495
   496
   497
   498  extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vfscanf")
   499
   500
   501
   502       __attribute__ ((__format__ (__scanf__, 2, 0))) ;
   503  extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vscanf")
   504
   505       __attribute__ ((__format__ (__scanf__, 1, 0))) ;
   506  extern int vsscanf (const char *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vsscanf") __attribute__ ((__nothrow__ , __leaf__))
   507
   508
   509
   510       __attribute__ ((__format__ (__scanf__, 2, 0)));
   511  # 485 "/usr/include/stdio.h" 3 4
   512  extern int fgetc (FILE *__stream);
   513  extern int getc (FILE *__stream);
   514
   515
   516
   517
   518
   519  extern int getchar (void);
   520
   521
   522
   523
   524
   525
   526  extern int getc_unlocked (FILE *__stream);
   527  extern int getchar_unlocked (void);
   528  # 510 "/usr/include/stdio.h" 3 4
   529  extern int fgetc_unlocked (FILE *__stream);
   530  # 521 "/usr/include/stdio.h" 3 4
   531  extern int fputc (int __c, FILE *__stream);
   532  extern int putc (int __c, FILE *__stream);
   533
   534
   535
   536
   537
   538  extern int putchar (int __c);
   539  # 537 "/usr/include/stdio.h" 3 4
   540  extern int fputc_unlocked (int __c, FILE *__stream);
   541
   542
   543
   544
   545
   546
   547
   548  extern int putc_unlocked (int __c, FILE *__stream);
   549  extern int putchar_unlocked (int __c);
   550
   551
   552
   553
   554
   555
   556  extern int getw (FILE *__stream);
   557
   558
   559  extern int putw (int __w, FILE *__stream);
   560
   561
   562
   563
   564
   565
   566
   567  extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
   568       ;
   569  # 603 "/usr/include/stdio.h" 3 4
   570  extern __ssize_t __getdelim (char **__restrict __lineptr,
   571                               size_t *__restrict __n, int __delimiter,
   572                               FILE *__restrict __stream) ;
   573  extern __ssize_t getdelim (char **__restrict __lineptr,
   574                             size_t *__restrict __n, int __delimiter,
   575                             FILE *__restrict __stream) ;
   576
   577
   578
   579
   580
   581
   582
   583  extern __ssize_t getline (char **__restrict __lineptr,
   584                            size_t *__restrict __n,
   585                            FILE *__restrict __stream) ;
   586
   587
   588
   589
   590
   591
   592
   593  extern int fputs (const char *__restrict __s, FILE *__restrict __stream);
   594
   595
   596
   597
   598
   599  extern int puts (const char *__s);
   600
   601
   602
   603
   604
   605
   606  extern int ungetc (int __c, FILE *__stream);
   607
   608
   609
   610
   611
   612
   613  extern size_t fread (void *__restrict __ptr, size_t __size,
   614         size_t __n, FILE *__restrict __stream) ;
   615
   616
   617
   618
   619  extern size_t fwrite (const void *__restrict __ptr, size_t __size,
   620          size_t __n, FILE *__restrict __s);
   621  # 673 "/usr/include/stdio.h" 3 4
   622  extern size_t fread_unlocked (void *__restrict __ptr, size_t __size,
   623           size_t __n, FILE *__restrict __stream) ;
   624  extern size_t fwrite_unlocked (const void *__restrict __ptr, size_t __size,
   625            size_t __n, FILE *__restrict __stream);
   626
   627
   628
   629
   630
   631
   632
   633  extern int fseek (FILE *__stream, long int __off, int __whence);
   634
   635
   636
   637
   638  extern long int ftell (FILE *__stream) ;
   639
   640
   641
   642
   643  extern void rewind (FILE *__stream);
   644  # 707 "/usr/include/stdio.h" 3 4
   645  extern int fseeko (FILE *__stream, __off_t __off, int __whence);
   646
   647
   648
   649
   650  extern __off_t ftello (FILE *__stream) ;
   651  # 731 "/usr/include/stdio.h" 3 4
   652  extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);
   653
   654
   655
   656
   657  extern int fsetpos (FILE *__stream, const fpos_t *__pos);
   658  # 757 "/usr/include/stdio.h" 3 4
   659  extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
   660
   661  extern int feof (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
   662
   663  extern int ferror (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
   664
   665
   666
   667  extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
   668  extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
   669  extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
   670
   671
   672
   673
   674
   675
   676
   677  extern void perror (const char *__s);
   678
   679
   680
   681
   682
   683  # 1 "/usr/include/bits/sys_errlist.h" 1 3 4
   684  # 26 "/usr/include/bits/sys_errlist.h" 3 4
   685  extern int sys_nerr;
   686  extern const char *const sys_errlist[];
   687  # 782 "/usr/include/stdio.h" 2 3 4
   688
   689
   690
   691
   692  extern int fileno (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
   693
   694
   695
   696
   697  extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
   698  # 800 "/usr/include/stdio.h" 3 4
   699  extern FILE *popen (const char *__command, const char *__modes) ;
   700
   701
   702
   703
   704
   705  extern int pclose (FILE *__stream);
   706
   707
   708
   709
   710
   711  extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__));
   712  # 840 "/usr/include/stdio.h" 3 4
   713  extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
   714
   715
   716
   717  extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
   718
   719
   720  extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
   721  # 858 "/usr/include/stdio.h" 3 4
   722  extern int __uflow (FILE *);
   723  extern int __overflow (FILE *, int);
   724  # 873 "/usr/include/stdio.h" 3 4
   725
   726  # 13 "test.c" 2
   727
   728
   729  # 14 "test.c"
   730  int main(int argc,char **argv[])
   731  {
   732   char sz[] = "Hello world!\n";
   733   printf("%s",sz);
   734   fflush(
   735  # 18 "test.c" 3 4
   736         stdout
   737  # 18 "test.c"
   738               );
   739
   740   return 0;
   741
   742  }

2,编译:生成相应的汇编代码文件

gcc对C生成的汇编文件是.s文件,编译命令为,

gcc -S test.i -o test.s

同样用cat -n test.s命令查看test.s内容

   1         .file   "test.c"
     2          .text
     3          .section        .rodata
     4  .LC0:
     5          .string "%s"
     6          .text
     7          .globl  main
     8          .type   main, @function
     9  main:
    10  .LFB0:
    11          .cfi_startproc
    12          pushq   %rbp
    13          .cfi_def_cfa_offset 16
    14          .cfi_offset 6, -16
    15          movq    %rsp, %rbp
    16          .cfi_def_cfa_register 6
    17          subq    $48, %rsp
    18          movl    %edi, -36(%rbp)
    19          movq    %rsi, -48(%rbp)
    20          movq    %fs:40, %rax
    21          movq    %rax, -8(%rbp)
    22          xorl    %eax, %eax
    23          movabsq $8031924123371070792, %rax
    24          movq    %rax, -22(%rbp)
    25          movl    $560229490, -14(%rbp)
    26          movw    $10, -10(%rbp)
    27          leaq    -22(%rbp), %rax
    28          movq    %rax, %rsi
    29          leaq    .LC0(%rip), %rdi
    30          movl    $0, %eax
    31          call    printf@PLT
    32          movq    stdout(%rip), %rax
    33          movq    %rax, %rdi
    34          call    fflush@PLT
    35          movl    $0, %eax
    36          movq    -8(%rbp), %rdx
    37          xorq    %fs:40, %rdx
    38          je      .L3
    39          call    __stack_chk_fail@PLT
    40  .L3:
    41          leave
    42          .cfi_def_cfa 7, 8
    43          ret
    44          .cfi_endproc
    45  .LFE0:
    46          .size   main, .-main
    47          .ident  "GCC: (GNU) 9.2.0"
    48          .section        .note.GNU-stack,"",@progbits

3,汇编:将汇编代码转变成二进制代码

gcc -c test.s -o test.o

查看二进制代码和其他不同,可以用hexdump来查看

  1 0000000 457f 464c 0102 0001 0000 0000 0000 0000
  2 0000010 0001 003e 0001 0000 0000 0000 0000 0000
  3 0000020 0000 0000 0000 0000 03b8 0000 0000 0000
  4 0000030 0000 0000 0040 0000 0000 0040 000d 000c
  5 0000040 4855 e589 8348 30ec 7d89 48dc 7589 64d0
  6 0000050 8b48 2504 0028 0000 8948 f845 c031 b848
  7 0000060 6548 6c6c 206f 6f77 8948 ea45 45c7 72f2
  8 0000070 646c 6621 45c7 0af6 4800 458d 48ea c689
  9 0000080 8d48 003d 0000 b800 0000 0000 00e8 0000
 10 0000090 4800 058b 0000 0000 8948 e8c7 0000 0000
 11 00000a0 00b8 0000 4800 558b 64f8 3348 2514 0028
 12 00000b0 0000 0574 00e8 0000 c900 25c3 0073 4700
 13 00000c0 4343 203a 4728 554e 2029 2e39 2e32 0030
 14 00000d0 0014 0000 0000 0000 7a01 0052 7801 0110
 15 00000e0 0c1b 0807 0190 0000 001c 0000 001c 0000
 16 00000f0 0000 0000 007b 0000 4100 100e 0286 0d43
 17 0000100 0206 0c76 0807 0000 0000 0000 0000 0000
 18 0000110 0000 0000 0000 0000 0000 0000 0000 0000
 19 0000120 0001 0000 0004 fff1 0000 0000 0000 0000
 20 0000130 0000 0000 0000 0000 0000 0000 0003 0001
 21 0000140 0000 0000 0000 0000 0000 0000 0000 0000
 22 0000150 0000 0000 0003 0003 0000 0000 0000 0000
 23 0000160 0000 0000 0000 0000 0000 0000 0003 0004
 24 0000170 0000 0000 0000 0000 0000 0000 0000 0000
 25 0000180 0000 0000 0003 0005 0000 0000 0000 0000
 26 0000190 0000 0000 0000 0000 0000 0000 0003 0007
 27 00001a0 0000 0000 0000 0000 0000 0000 0000 0000
 28 00001b0 0000 0000 0003 0008 0000 0000 0000 0000
 29 00001c0 0000 0000 0000 0000 0000 0000 0003 0006
 30 00001d0 0000 0000 0000 0000 0000 0000 0000 0000
 31 00001e0 0008 0000 0012 0001 0000 0000 0000 0000
 32 00001f0 007b 0000 0000 0000 000d 0000 0010 0000
 33 0000200 0000 0000 0000 0000 0000 0000 0000 0000
 34 0000210 0023 0000 0010 0000 0000 0000 0000 0000
 35 0000220 0000 0000 0000 0000 002a 0000 0010 0000
 36 0000230 0000 0000 0000 0000 0000 0000 0000 0000
 37 0000240 0031 0000 0010 0000 0000 0000 0000 0000
 38 0000250 0000 0000 0000 0000 0038 0000 0010 0000
 39 0000260 0000 0000 0000 0000 0000 0000 0000 0000
 40 0000270 7400 7365 2e74 0063 616d 6e69 5f00 4c47
 41 0000280 424f 4c41 4f5f 4646 4553 5f54 4154 4c42
 42 0000290 5f45 7000 6972 746e 0066 7473 6f64 7475
 43 00002a0 6600 6c66 7375 0068 5f5f 7473 6361 5f6b
 44 00002b0 6863 5f6b 6166 6c69 0000 0000 0000 0000
 45 00002c0 0043 0000 0000 0000 0002 0000 0005 0000
 46 00002d0 fffc ffff ffff ffff 004d 0000 0000 0000
 47 00002e0 0004 0000 000b 0000 fffc ffff ffff ffff
 48 00002f0 0054 0000 0000 0000 0002 0000 000c 0000
 49 0000300 fffc ffff ffff ffff 005c 0000 0000 0000
 50 0000310 0004 0000 000d 0000 fffc ffff ffff ffff
 51 0000320 0075 0000 0000 0000 0004 0000 000e 0000
 52 0000330 fffc ffff ffff ffff 0020 0000 0000 0000
 53 0000340 0002 0000 0002 0000 0000 0000 0000 0000
 54 0000350 2e00 7973 746d 6261 2e00 7473 7472 6261
 55 0000360 2e00 6873 7473 7472 6261 2e00 6572 616c
 56 0000370 742e 7865 0074 642e 7461 0061 622e 7373
 57 0000380 2e00 6f72 6164 6174 2e00 6f63 6d6d 6e65
 58 0000390 0074 6e2e 746f 2e65 4e47 2d55 7473 6361
 59 00003a0 006b 722e 6c65 2e61 6865 665f 6172 656d
 60 00003b0 0000 0000 0000 0000 0000 0000 0000 0000
 61 *
 62 00003f0 0000 0000 0000 0000 0020 0000 0001 0000
 63 0000400 0006 0000 0000 0000 0000 0000 0000 0000
 64 0000410 0040 0000 0000 0000 007b 0000 0000 0000
 65 0000420 0000 0000 0000 0000 0001 0000 0000 0000
 66 0000430 0000 0000 0000 0000 001b 0000 0004 0000
 67 0000440 0040 0000 0000 0000 0000 0000 0000 0000
 68 0000450 02c0 0000 0000 0000 0078 0000 0000 0000
 69 0000460 000a 0000 0001 0000 0008 0000 0000 0000
 70 0000470 0018 0000 0000 0000 0026 0000 0001 0000
 71 0000480 0003 0000 0000 0000 0000 0000 0000 0000
 72 0000490 00bb 0000 0000 0000 0000 0000 0000 0000
 73 00004a0 0000 0000 0000 0000 0001 0000 0000 0000
 74 00004b0 0000 0000 0000 0000 002c 0000 0008 0000
 75 00004c0 0003 0000 0000 0000 0000 0000 0000 0000
 76 00004d0 00bb 0000 0000 0000 0000 0000 0000 0000
 77 00004e0 0000 0000 0000 0000 0001 0000 0000 0000
 78 00004f0 0000 0000 0000 0000 0031 0000 0001 0000
 79 0000500 0002 0000 0000 0000 0000 0000 0000 0000
 80 0000510 00bb 0000 0000 0000 0003 0000 0000 0000
 81 0000520 0000 0000 0000 0000 0001 0000 0000 0000
 82 0000530 0000 0000 0000 0000 0039 0000 0001 0000
 83 0000540 0030 0000 0000 0000 0000 0000 0000 0000
 84 0000550 00be 0000 0000 0000 0012 0000 0000 0000
 85 0000560 0000 0000 0000 0000 0001 0000 0000 0000
 86 0000570 0001 0000 0000 0000 0042 0000 0001 0000
 87 0000580 0000 0000 0000 0000 0000 0000 0000 0000
 88 0000590 00d0 0000 0000 0000 0000 0000 0000 0000
 89 00005a0 0000 0000 0000 0000 0001 0000 0000 0000
 90 00005b0 0000 0000 0000 0000 0057 0000 0001 0000
 91 00005c0 0002 0000 0000 0000 0000 0000 0000 0000
 92 00005d0 00d0 0000 0000 0000 0038 0000 0000 0000
 93 00005e0 0000 0000 0000 0000 0008 0000 0000 0000
 94 00005f0 0000 0000 0000 0000 0052 0000 0004 0000
 95 0000600 0040 0000 0000 0000 0000 0000 0000 0000
 96 0000610 0338 0000 0000 0000 0018 0000 0000 0000
 97 0000620 000a 0000 0008 0000 0008 0000 0000 0000
 98 0000630 0018 0000 0000 0000 0001 0000 0002 0000
 99 0000640 0000 0000 0000 0000 0000 0000 0000 0000
100 0000650 0108 0000 0000 0000 0168 0000 0000 0000
101 0000660 000b 0000 0009 0000 0008 0000 0000 0000
102 0000670 0018 0000 0000 0000 0009 0000 0003 0000
103 0000680 0000 0000 0000 0000 0000 0000 0000 0000
104 0000690 0270 0000 0000 0000 0049 0000 0000 0000
105 00006a0 0000 0000 0000 0000 0001 0000 0000 0000
106 00006b0 0000 0000 0000 0000 0011 0000 0003 0000
107 00006c0 0000 0000 0000 0000 0000 0000 0000 0000
108 00006d0 0350 0000 0000 0000 0061 0000 0000 0000
109 00006e0 0000 0000 0000 0000 0001 0000 0000 0000
110 00006f0 0000 0000 0000 0000                    
111 00006f8

4,链接:主要是为了解决多个文件之间符号引用的问题

gcc test.o -o test

链接完成以后生成目标文件test,命令./test运行test文件(如果有多个目标文件的话可以写在一起用空格隔开,gcc test1.o test2.o test3.o -o test)

Hello world!

如此,gcc内部工作的几个阶段就此完成了,当然实际上并没有如此麻烦,只需要gcc test.c -o test就能生成可执行文件

 

gcc编译的过程

标签:stat   put   实践   保存文件   tde   type   pid_t   end   error   

原文地址:https://www.cnblogs.com/coherent3c/p/12245983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!