Now I will use my understanding to explain the running process of a helloworld program:
// ---------- Helloworld. cpp
# Include <stdio. h>
Int main (){
Printf ("Hello world! \ N ");
Return 0;
}
From helloworld. cpp to helloworld.exe, the following process is performed:
1. the compiler performs some preprocessing, such as processing the # include <stdio. h> content of stdio. H into the text, and processing the # define Compilation instruction.
2. Compile the pre-processed source program and translate C into assembly code.
3. Compile: Compile the above assembly code, and then compile the code into the intermediate code file obj. The OBJ file contains the machine code when the program is running, there are also Data Structures prepared for the linker and so on, but in some cases it does not have the address of some functions, just replaced by symbols.
4. Link: connect intermediate files. The program uses a lot of other code and uses a lot of libraries (C Runtime libraries), such as the printf function. You didn't implement it, printf is only stored in other places. The linker needs to find out its Code address, and then modify the absolute address of the machine code jump accordingly (here the absolute address is a virtual address ). then generate the EXE file. through the linker, the machine code of the program can be run on the operating system.
The process on Linux is the same.
From Double-hitting helloworld.exe to program termination
1. Create the helloworld process in the cmder.exe shell in windows.
2. In Windows, allocate resources to the process, create the main thread, start the C function, initialize the program, and then call the main function of helloworld. when main returns, call exit to end the program and return it to the operating system.
After reading the <in-depth understanding of computer systems> and other materials, I wrote down my understanding of the program running. This is just my understanding. Is there any error? Please point out my misunderstandings.