How does the C language program you write run?

Table of contents

 translation environment

compile

Precompiled

 compile

compilation 

Link

operating environment


We all know that computers can only recognize binary instructions, so how do computers recognize the code we write?

In any implementation of ANSI C (standard C), there are two distinct environments.
The first is the translation environment, where source code is converted into executable machine instructions.
The second is the runtime environment, which is used to actually execute the code.

 translation environment

  •  Each source file that makes up a program is individually converted into a corresponding object file through a compilation process .
  • Each object file is bundled together by the linker to form a single, complete executable program .
  • The linker will also import any functions used by the program in the standard C function library, and it can search the programmer's personal program library and link the functions it needs into the program

The translation environment can be divided into:

compile

  • Precompiled
  • compile
  • compilation

Link



compile

Precompiled

Precompilation will generate a file with the suffix .i. The left picture is the source program, and the right picture is the precompiled program (the top few lines are references to the header file).

All the program needs to do in the precompilation phase are some text operations:

  1. Delete all comments in the code;
  2. Replace all macros and identifiers defined by #define;
  3. Include all header files;
  4. All preprocessing directives are processed.

 compile

After the code is processed in the pre-compilation stage, it enters the compilation stage. At this stage, the code will be translated into assembly code and placed in a file with a suffix of .s.

 This is the assembly code in the .s file generated by the above code after the compilation stage.

During the compilation phase the program performs the following operations:

  • Gramma analysis
  • lexical analysis
  • Semantic Analysis
  • Symbol summary

compilation 

The code is converted to binary code during the assembly stage.

The target files and executable programs generated by gcc compilation under Linux are all stored in ELF format. And this format can be recognized by the following command in gcc (readelf test.o -a):

 During the assembly phase the program performs the following operations:

  • Form a symbol table;
  • Convert assembly code to binary code.

 What do the symbols mean?

In the compilation stage, there is a symbol summary. The following code will generate a symbol table after symbol summary, as shown in the figure:

Link

In the linking stage, the program will be converted from the .o file into an executable program.

 During the linking phase the program does the following:

  • Merge segment table;
  • Synthesis and relocation of symbol tables.

 Since each source file will generate a symbol table separately but there is only one executable program, the symbol table will be merged to merge the symbols with the same name and replace the invalid address. If the above program distance is in the impgame.c file If the ADD function is not defined, the program will report an error at this time. That is to say, the function is found in the linking phase that the called function is undefined.



operating environment

The process of program execution:

  1. The program must be loaded into memory. In an environment with an operating system: Generally this is done by the operating system. In a standalone environment, program loading must be arranged manually, possibly by placing executable code into read-only memory.
  2. Program execution begins. Then call the main function.
  3. Start executing program code. At this time, the program will use a runtime stack (stack) to store the local variables and return address of the function. Programs can also use static memory. Variables stored in static memory retain their values ​​throughout the execution of the program.
  4. Terminate the program. Normal termination of the main function; unexpected termination is also possible.
     

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/131816300