C language compilation process

  Edit hello.c, take this file as an example 

1 #include <stdio.h>
2 #include <stdlib.h>
3 int main()
4 {
5         printf("hello world!\n");
6         return 0;
7 }

[The first step] Preprocessing hello.c---hello.i

    The preprocessing process is essentially to process ''#'', copy the header file included in #include directly to hello.c; replace the macro defined by #define, and delete the useless comment part in the code, etc. 

    The specific things are as follows:

    (1) Delete all #defines and expand all macro definitions.

    (2) Process all conditional compilation instructions, #ifdef #ifndef #endif, etc., those with #

    (3) Process #include and insert the file pointed to by #include into the line

    (4) Delete all comments

    (5) Add line numbers and file labels, so that when debugging and compiling errors, you will know which line of which file is 

    (6) Reserve #pragma compiler directives, because the compiler needs to use them

 

    [Step 2] Compile hello.i---hello.s

    The process of compilation is essentially the process of translating high-level language into machine language

    (1) Lexical analysis

    (2) Syntax analysis

    (3) Semantic Analysis

    (4) Generate corresponding assembly code after optimization

    From high level language -> assembly language -> machine language (binary)

 

    [Step 3] Assemble hello.s---hello.o

    The assembler as translates hello.s into machine language and saves it in hello.o (binary text form)

 

    【Step 4】Link hello.o--- hello

    Just like the hello.c just now, it uses the "printf" of the C standard library, but the compilation process just translates the source file into a binary. This binary cannot be executed directly. At this time, an action needs to be done.

    Bind the translated binary with the required libraries.

 

    You can also directly use the following command to complete the compilation

         gcc hello.c -o hello can generate an executable program. i.e. gcc takes no arguments. ldd can see the libraries your executable program depends on

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324773226&siteId=291194637