In Windows systems, you can double-click a file with the suffix ".exe" to run it. During programming, the compiled final result is generally expressed as an EXE program and other DLL that provides support for program execution. When we double-click an EXE program, what is done on the operating system level to make the application executable?
Now there is an app.exe file. I have summarized this article. After double-hitting app.exe, the operating system will do the following:
1. shellcalls createprocssto activate an app.exe process. Shellis a command interpreter. It is a system process that is started during the operating system boot. in the Windows Task Manager, you can see a process named mongoer.exe.
2. createprocss creates a process kernel object, and the system creates a 4 GB virtual address space for the process (2 Gbit/s private address space for each process under win2000/winxp, and the remaining 2 Gbit/s is occupied by the operating system for app.exe and other necessary DLL functions;
3. CreateProcess loads the EXE file and analyzes the file header (for the specific format, see PE File Format analysis) to identify the file running environment. The file header determines which environment to load;
4. After app.exe and necessary DLL file data and code, createprocss creates the main thread and executes the startup code during the C/C ++ runtime. The remaining processes are executed by the startup code.
From the above description, we can see that the first function actually called by a program should be the startup function at the runtime of C/C ++. So what role does the C/C ++ Runtime Library play when the program is running? The following are some learning experiences about the C/C ++ runtime.
What is the C/C ++ Runtime Library? You can search for it on the Internet to get a large string of results. The Runtime Library is a library, and the program code we write everyday runs on this library. The Runtime Library completes some underlying basic work, for example, initialize the memory unit allocation function during the runtime, initialize the memory stack used by the underlying I/O routine, and initialize the global variables for the C/C ++ runtime, call constructor for C ++ global and static classes. Such a Runtime Library frees programmers from focusing too much on the underlying content and on their own application logic. The Runtime Library also provides some basic library function calls, such as memcpy and malloc. More importantly, the Runtime Library also adds a startup function for the application.
In Windows, the C run-time library provided by VC is divided into dynamic Runtime Library and static Runtime Library. The dynamic Runtime Library is msvcrt. dll (Debug: msvcrtd. dll), and the corresponding library file is msvcrt. Lib (Debug: msvcrtd. Lib ). The main files corresponding to the static Runtime Library are: libc. Lib (single-thread static library) and libcmt. Lib (multi-thread static library ). Msvcrt. dll provides thousands of C functions, including low-level functions such as printf, which are also in msvcrt. dll.
When an application is compiled and linked, the compiler uses the compilation options (Project Settings in Visual Studio), such as single-thread, multi-thread, or DLL, automatically links the startup functions of different runtime libraries for the application. In vs2005, you can view or modify the options through the following operations to determine which Runtime Library to link:
Open project properties, select Configuration properties on the left --> C/C ++ --> code generation, view the Runtime Library, and select different runtime libraries.
Back to the first question, the main thread executes the startup code during the C/C ++ runtime. the startup function calls the corresponding entry point function and enters the application to execute the code logic.
If you do not use the wide-byte console program, the startup function is maincrtstartup. This function is in the CRT \ SRC \ crt0.c file under the VC installation directory. The following is a simplified version found on the Internet:
Void maincrtstartup (void) {int mainret;/* obtain the complete Win32 version information */_ osver = getversion (); _ winminor = (_ osver> 8) & 0 × 00FF; _ winmajor = _ osver & 0 × 00FF; _ winver = (_ winmajor <8) + _ winminor; _ osver = (_ osver> 16) & 0 × 00ffff; _ ioinit ();/* initialize lowio * // * obtain command line information */_ acmdln = (char *) getcommandlinea (); /* obtain the environment information */_ aenvptr = (char *) _ crtgetenvironmentstringsa (); _ setargv ();/* set the life cycle Make the line parameter */_ setenvp ();/* set the Environment Parameter */_ cinit ();/* C data initialization: Initialize the global variable, right here! */_ Initenv = _ environ; mainret = Main (_ argc, _ argv, _ environ);/* call the main function */exit (mainret );}
The code above shows that the Runtime Library performs initialization before calling the main or winmain functions of the user program. After Initialization is complete, the main or winmain function we wrote is called. In this way, the C/C ++ Runtime Library and application work normally.
In addition to crt0.c, the C Runtime Library also contains wcrt0.c, wincrt0.c, and wwincrt0.c files to provide initialization functions. Wcrt0.c is a wide character set version of crt0.c. wincrt0.c contains entry functions of Windows applications, while wwincrt0.c is a wide character set version of wincrt0.c.
The code of the simplified version above also shows that when the main or winmain function of the user program is executed, the return value is passed into the exit function as a parameter, and the exit function completes the program execution, including C ++ global and static classes, calling the exitprocess function of the operating system, and notifying the process to exit.
From: http://www.cnblogs.com/BpLoveGcy/archive/2010/03/24/1694240.html