any kind of implementation in ANSI C , there are two environments, the translation environment and the execution environment.
First, the translation environment
In the translation environment, the source code is turned into a machine instruction.
The translation consists of several steps, each of which consists of a program that is converted to the object code by the compilation process, respectively. The target files are then bundled together by the linker to form a single, complete executable program. The linker also introduces functions that are used by any of the modified programs in the standard library, including the functions that programmers introduce into their own defined header files. This process occurs during the preprocessing phase (adding header files, replacing instructions, replacing macros)
① pretreatment
This is the first stage of the compilation process, which makes a copy of the header file referenced by a program in the program, replaces the macro, and executes some text operations on the source code.
② parsing
After the compilation phase, the program enters the parsing phase, which generates error reports and warnings during the parser phase, and optimizes the process to make the program more efficient in the process of specifying optimization.
③ Generating the target file
One or more target files may be generated at this time
④ Links
We link several target files through the linker
* * The compiler for C in UNIX systems is called CC, and it can be called in many different ways.
1. Compile a C source code
EG:CC PROGRAM.C
GCC PROGRAM.C
These two instructions produce the same results, and they generate a a.out executable program. A target file named PROGRAM.O is generated in the middle, but it is deleted after the link process is complete.
2. Link several source files simultaneously
EG:CC 1.c 2.c 3.c
3. Compile a C source file and link it to an existing executable file
EG:CC main.c LOOKUP.O sort.c
4. Compile a source file and make it generate a specified executable file
EG:CC Main.c-o Test
At this point you will find that the resulting executable is no longer called a.out but is called Test.
Second, the implementation
① program loaded into memory
In an environment with an operating system, the process is done by the operating system. Must be done manually in a stand-alone environment.
② initializing a variable that is not initialized
Static variables are initialized by default, while other variables that are not initialized are initialized to "garbage"
③ Small Boot Program
It handles a bunch of routine tasks, such as collecting command-line parameters.
④ Call the main function
Termination of the ⑤ program
This is the last stage of execution, which may be caused by a different cause, and the normal termination is that the program is the return of the main function.
The program may be terminated by a user pressing the break key or it may be a hang of the phone connection, or it may be self-interrupting due to an error.
Knowledge Link:a.out is an abbreviated format for "assembler output", representing the assembler outputs. In earlier versions of Unix-like systems, A.out was an output format for executable files, target files, and shared libraries. The early PDP-7 system does not have a linker, the process of creating the program is to first connect all the source files into a file, and then compile, the resulting assembler saved in the a.out. This a.out is a veritable assembly output, but to PDP-11, people write a linker for it, the program is created first compiled and then the link output is saved to a.out, then a.out is actually the link output, but the output of the executable file still continues the naming habit.
Environment of the program