The strongest C language tutorial in history ---- program compilation and preprocessing (1)

content

1. The translation environment and execution environment of the program

2. Detailed compilation + linking

2.1 Translation environment

2.2 The compilation itself is also divided into several stages

2.3 Operating Environment


1. The translation environment and execution environment of the program

In any implementation of ANSI C, there are two distinct environments.

The first is a translation environment, in which source code is translated into executable machine instructions. The second is the execution environment, which is used to actually execute the code.

2. Detailed compilation + linking

2.1 Translation environment

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

2.2 The compilation itself is also divided into several stages

test.c code:

#include<stdio.h>

//声明函数
extern int Add(int x,int y);
int main()
{
    int a = 1;
    int b = 2;
    int ret = Add(a,b);
    printf("%d\n",ret);
    return 0;
}

 Add.c code:

int Add(int x,int y)
{
    return x+y;
}

2.3 Operating Environment

The process of program execution:

  1. The program must be loaded into memory. In an environment with an operating system: Usually this is done by the operating system. In a stand-alone environment, the loading of programs must be arranged manually, possibly by placing executable code into read-only memory.
  2. Execution of the program begins. Then call the main function.
  3. Start executing program code. At this point the program will use a run-time stack (stack) (function stack frame) to store the function's local variables and return addresses. Programs can also use static memory. Variables stored in static memory retain their values ​​throughout the execution of the program.
  4. Terminate the program. Terminates the main function normally; it may also terminate unexpectedly.

Guess you like

Origin blog.csdn.net/m0_57304511/article/details/123164352