The process of compiling an C program under Linux

  • 2020-05-07 20:07:08
  • OfStack

Learning a language program, I think we still have to learn its compilation rules, now, through a small example summary of their own understanding of C compilation.


/*test.c    To understand C Program compilation */

#include <stdio.h>
int main(void)
{
 printf("Hello World!\n");
 return 0;
}

For test.c, the command we usually use in step 1 is:


gcc -o test test.c  or  gcc test.c -o test

In fact, the above compilation command contains four stages of processing: preprocessing (also known as precompilation, Preprocessing), compilation (Compilation), assembly (Assembly), and wiring (Linking).

The full compilation process is detailed here

pretreatment:

Role: the role of preprocessing is primarily to read in the source code, examine the statements and macro definitions that contain the preprocessing instructions, and translate the response to the source code. Preprocessing also removes comments and excess white space characters from the program.

Object: the preprocessing instruction starts with "#", and the preprocessing object mainly includes the following aspects:

(1) #define macro definition

(2) the # operator       # converts the parameters that follow to a string.


      /*** case ***/
      #define PASTE(n) "adhfkj"#n
      int main()
      {
         printf("%s\n",PASTE(15));
         return 0;
      }
      /******** The output adhfj15*********/

(3) the ## operator is used to connect arguments to 1.


    /***** case *****/
    #define NUM(a,b,c) a##b##c
    #define STR(a,b,c) a##b##c
    int main()
     {
       printf("%d\n",NUM(1,2,3));
       printf("%s\n",STR("aa","bb","cc"));
       return 0;
     }
  /********* The final output of the program is :aabbcc**********/

(4) conditional compilation instruction

(5) the header file contains instructions

(6) special symbols

S 44en__ contains the string of the current program file name

S 47en__ represents the integer of the current line number

S 50en__ with the string of the current date

S 53en__ contains the current string

The preprocessing instruction for the test.c file above is


gcc -E test.c -o test.i

Compile - to compile into assembly language


gcc -S test.i -o test.s

This is what test.s compiled from the above code


.file "test.c"
 .section .rodata
.LC0:
 .string "hello world"
 .text
.globl main
 .type main, @function
main:
.LFB0:
 .cfi_startproc
 pushq %rbp
 .cfi_def_cfa_offset 16
 .cfi_offset 6, -16
 movq %rsp, %rbp
 .cfi_def_cfa_register 6
 movl $.LC0, %edi
 call puts
 movl $0, %eax
 leave
 .cfi_def_cfa 7, 8
 ret
 .cfi_endproc
.LFE0:
 .size main, .-main
 .ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)"
 .section .note.GNU-stack,"",@progbits

assembly

Action: compile the above assembly instructions to generate the target file


gcc -c test.s -o test.o

This is the contents of the test.o file above


ELF  >  8 @ @ 
 UH � ? ? ?  Good at  hello world GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)  zR x ?    A?C P  .symtab .strtab .shstrtab .rela.text .data .bss .rodata .comment .note.GNU-stack .rela.eh_frame   @     ? 0    &   X  ,   X  1   X  9  0 d -   B  ?  W   ? 8  R  ?       ? a    x      �      ?                    test.c main puts  
 ?  �   �   �    

link

The main purpose of linking is to link the target file of the program with the additional target file needed, and finally generate an executable file. The additional target file also includes the required library files (static link libraries and dynamic link libraries)


gcc test.o -o test

The resulting test file is the final file that the system can execute.

For the compilation of the program, we generally think of it as "compile" and "link" two parts are enough, the compilation here has included the preprocessing, compiled into assembly language and compiled into the object file 3 steps. As long as the header file is complete and syntax is correct, compile 1 should pass. Links can also be successful as long as there are complete target files and feature library files. As long as the compile passes and the link passes, the compilation of the entire project is complete.


Related articles: