C language helloworld process from running to ending (my understanding)

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.