C language-program compilation + link (linux+gcc implementation process)


Preface

To run a program, it must go through four steps. The sequence is: preprocessing, compilation, assembly and linking. Then we will disassemble these processes in turn to better understand how the program works.

First, briefly describe the content of each step:

  • Preprocessing: expand header file/macro replacement/remove comments/conditional compilation
  • Compile: check grammar, generate assembly code
  • Assembly: Convert assembly code into binary machine code and write it into target file
  • Link: Link each module to generate an executable program

Let's start the discussion later————


1. Pretreatment

1. Operations in the pretreatment stage:

  1. Expand all macro definitions and delete #define
  2. Delete all notes
  3. Keep all #pragma compilation directives, because the compiler needs to use them.
  4. Processing Conditional Compilation Instructions
  5. Process the #include precompilation directive, insert the included header file into the position of the compilation directive
  6. Add line number and file name

1. Realization of pretreatment process:

You can use linux to realize the process of preprocessing to the link and observe

Usage of gcc instruction

-E option: Prompt the compiler to stop after executing the preprocessing, and not execute the following compilation, assembly, and linking.
-S option: Prompt the compiler to stop after executing the compilation, not to execute assembly and linking.
-c option: Prompt the compiler to stop when it finishes executing the assembly.
If you use the gcc command without any options, the entire process of preprocessing, compilation, assembly, and linking will be performed by default. If the program is correct, you will get an executable file, the default is a.out, use -o + file name The name of the executable file can be defined.

A simple code understanding preprocessing

Insert picture description here
First, edit a test.c file, which contains comments, macro definitions and pre-compiled instructions, and execute gcc -E test.c -o test.iit to generate a test.i file.
Insert picture description here

Observe that the file has become about 900 lines, the macro definition has been replaced, and the comments have also been deleted. The above part is the code contained in stdio.h.

Two, compile

1. Operations during the compilation phase:

The compilation process is the core part of the entire program construction. If the compilation is successful, the source code will be converted from text form into machine language. The compilation process is to perform a series of lexical analysis, syntax analysis, semantic analysis and optimization on the preprocessed file to generate the corresponding The assembly code file.

2. The realization of the compilation process:

The simple code above is still executed against the generated test.i file to gcc -S test.i -o test.sgenerate a test.s file.
Insert picture description here
Observe that the test.s file has been converted into assembly code from the previous text form.

Three, compilation

1. Operations in the assembly stage:

The assembly process calls the assembler as to complete, which is used to convert the assembly code into instructions that can be executed by the machine. Each assembly statement almost corresponds to a machine instruction.

2. The realization of the assembly process:

Execute against the generated test.s file gcc -c test.s -o test.oand generate a test.o file.
Insert picture description here
Files will become such a language that only machines can understand.

Fourth, the link

1. The operation of the link phase:

The main content of the link is to correctly connect the mutually referenced parts between the various modules. Its job is to amend the references of some instructions to the addresses of other symbols. The linking process mainly includes address and space allocation, symbol resolution and redirection.

2. The realization of the linking process:

Use gcc test.o -o testinstructions to generate a test file.
Insert picture description here
This is the linked file, and it is also an executable program. You can see the final answer by ./testexecuting
Insert picture description here
it.

Guess you like

Origin blog.csdn.net/weixin_47460769/article/details/113832389