C and C + + program startup code (startup)

Source: Internet
Author: User

C or C + + language, the entry function on materializing is main (ARGC,ARGV), or Tmain, WMAin, WinMain, and so on. Further, it's easy to know that the void maincrtstartup(void) function in C Runtime's startup code calls the main function written by the programmer. This function is defined in a. C file under the Crt\src\ directory of the Visual C + + installation directory (depending on the version of VC + +, the files are stored differently). It performs some initialization operations, such as getting command-line arguments, getting the value of an environment variable, initializing a global variable , initializing the required preparation of the IO, and calling Main (ARGC,ARGV). After the main function returns, mainCRTStartup also needs to call the global variable's destructor or some of the functions registered by Atexit ().
In depth, it is the entry that tells the linker that the executable is a maincrtstartup when the link is delivered as an executable file. Of course, programmers can also specify the entry of the program at the command line at the time of the link or in the source program, for example:

thus, when the executable is executed, the operating system creates the process and the main thread, starting with the mainCRTStartup to execute the main thread.

The initialization preparation work done by mainCRTStartup, such as getting command-line arguments, getting the environment variable values, is implemented by invoking the appropriate Windows system call. However, initializing a global variable is worth thinking about. In what order are many global variables initialized and destroyed in what order? How do programmers specify the order in which global variables are initialized? What is the implementation mechanism? The answer is MSDN for the C language preprocessing pragma directive init_seg help article or I wrote on the Baidu Encyclopedia Init_seg article.

Simply put, the compiler treats the entry address of the initialization code of the global variable as a function pointer (i.e. void (*FP) () of a parameterless non-return value type, which is stored sequentially in the executable file. A function pointer table is formed in the CRT section. mainCRTStartup iterates through the array of function pointers by calling the _initterm function (defined in the cinitexe.c file), invoking each function pointer in turn to complete the initialization of the global variable.

Then, how to specify the global variables to initialize these function addresses in. What is the order of storage in the CRT section? This is a little common sense of the linker, for. CRT$XYZ or. CRT$UVW such a section name, in front of which is the final section name after the link, the linker merges the. obj files in these same-named sections, whereas $ after these strings are told to the linker to merge the same identically named sections in the dictionary order of the strings that follow. Two sections have been defined in the C Runtime library:. Crt$xca and. Crt$xcz as the table header and footer for the function pointer table. The compiler defaults to placing the initialization code address of the global variable. The CRT$XCU. The programmer can also put the address of the initialization code of the global variable written by himself into "XCA" and "XCZ". For example:

#pragma init_seg (". CRT$XCM ")

from this line to the end of the compilation unit (translate unit) , all the initialization code entry addresses for global variables defined within this scope are placed in the. crt$xcm section of the. obj file. If, the other compiler unit's global variable initialization code entry address is placed. CRT$XCN section, the two compilation units are separated in the global variable initialization, of course, "XCM" before "XCN".

When programming, we can completely imitate the above algorithm and data structure, and define the initialization and destruction of global variables. For example, build a. mycrt section as a function pointer table. When defining a global variable, tell the compiler by #pragma init_seg (". Mycrt", Dtorregister) Insert the global variable initialization code entry address into the. Mycrt section, its destructor code entry address is registered in a global function pointer array that the programmer customizes by invoking the Dtorregister function written by the programmer himself. This allows you to iterate through the execution of global variable initialization at the time the programmer chooses, or to traverse the dtor that executes the global variable in turn.

With regard to C and C + + 's startup code, the most authoritative is Matt Pietrek published in the 1th issue of MSDN Magazine 2001, "under the Hood:reduce EXE and DLL size with Libctiny." LIB ". He wrote a very concise, so very easy to read tiny C runtime library, including the startup, as well as printf such a C standard library function implementation, can be compiled with simple C source program linked to the size of the super small executable file, The size of an executable file that is comparable to the same functionality that is written directly with sinks. This article and source code on the Internet is easy to search.

Finally, you need to know thatC and C + + programs have a special treatment for the main function. CPP file, you do not need to point out that it is extern "C", which has this property by default . Second, either int main (), void Main (), or Main (ARGC,ARGV), Main (argc,argv,env), or if the _UNICODE macro is already defined, the main function is still used instead of the wmain (). will be able to get the correct running results. I've also found that the exit () function is also very special, without the need for a header file to compile and run normally. Although exit () is declared in STDLIB.H.

C and C + + program startup code (startup)

Related Article

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.