C Runtime Library and a deep understanding of the meaning of compilation options-01

Source: Internet
Author: User
The following content is from the network and the author is not clear. Transferred from:Http://advancedcj.wordpress.com/2010/06/18/c-runtime-library-1/

Generation 1

The Runtime Library is Program Library files required during runtime are usually provided in the form of LIB or DLL. The C Runtime Library is the c run-time library, which was born in 1970s. It is a concept in the C rather than the C ++ language world. The functions in these libraries are required when C Programs are running. C language is a so-called "Small kernel" language, which is small in itself (not many keywords, program flow control, data type, etc ). Therefore, after the C language kernel is developed, Dennis Ritchie and Brian kernighan use C to rewrite more than 90% of UNIX system functions, and separate the most commonly used functions, the header file and corresponding library are formed, and the C Runtime Library is formed in this way. With the prevalence of C language, the manufacturers, individuals, and groups of C compilers all follow the old tradition and have corresponding standard libraries on different platforms, however, most implementations are related to various platforms. Since each C compiler has many differences and subtle differences in C support and understanding, it has ansi c; ansi c (subjective intention) it specifies the specific meaning of each element of C language and compiler implementation requirements in detail, introduces a new function declaration method, and sets up the standard form of standard library. Therefore, the C Runtime Library is provided by the compiler manufacturer. The header files and library functions provided by other vendors/individuals/groups should be called the third-party C Runtime Library (Third Party C run-time libraries ). 2 Development In the C ++ world, there is another concept: Standard C ++ library, which includes the C run-time library and STL mentioned above. The reason for including C run-time library is obvious. c ++ is the superset of C, and there is no reason to re-create a C ++ run-time library. the standard C ++ Library added by VC for C ++ mainly includes libcp. lib, libcpmt. lib and msvcprt. when the Lib C Runtime Library was born, the program world was still simple. Applications were all single-threaded, and the multi-task or multi-thread mechanism was still a new concept. Therefore, the C Runtime Library in this period is single-threaded. With the development of multi-thread technology in the operating system, the initial C Runtime Library could not meet the needs of the program and encountered a serious problem. The C Runtime library uses multiple global variables (such as errno) and static variables, which may cause conflicts in multi-threaded programs. If both threads set errno at the same time, the result is that the later errno will overwrite the previous one, and the user cannot get the correct error message. Therefore, Visual C ++ provides two versions of the C Runtime Library. One version can be called by a single-threaded application, and the other version can be called by a multi-threaded application. There are two major differences between the multi-thread Runtime Library and the single-thread Runtime Library: (1) global variables similar to errno, each thread sets a separate one. In this way, the correct error information can be obtained from each thread. (2) Data Structures in multi-threaded databases are protected by synchronization mechanisms. This avoids access conflicts. The multi-thread Runtime Library provided by Visual C ++ is divided into static and dynamic link libraries, and each type of Runtime Library can be further divided into debug and release versions, therefore, Visual C ++ provides six runtime libraries. The following table shows: (single-thread and multi-thread versions have both Debug and release versions. Only multi-thread versions have static and dynamic link libraries.) The single thread (static link) libc file of the C Runtime Library. libdebug single thread (static link) libcd. libmultithread (static link) libcmt. libdebug multithread (static link) libcmtd. libmultithread (Dynamic Link) msvcrt. lib (import to database) debug multithread (Dynamic Link) msvcrtd. lib (import to database) Role of the 3C Runtime Library (1) The C Runtime Library contains the most basic and commonly used functions (such as memcpy, printf, and malloc) for the C program to run ). (2) In addition to providing necessary library function calls, the C Runtime Library also provides the most important function to add a startup function for the application. The main function of the C Runtime Library startup function is to initialize the program, assign the initial value to the global variables, and load the user program's entry function. The entry point of the console program that does not use the wide character set is maincrtstartup (void ). Next we will take this function as an example to analyze what kind of Entry Program is added to the Runtime Library for us. This function is defined in crt0.c. Code After the author's arrangement and simplification: Void Maincrtstartup ( Void )
{
Int Mainret;
/* Obtain the complete Win32 version information. */
_ Osver = Getversion ();
_ Winminor = (_ Osver > &   0x00ff ;
_ Winmajor = _ Osver &   0x00ff ;
_ Winver = (_ Winmajor < + _ Winminor;
_ Osver = (_ Osver >   16 ) &   0x00ffff ;
_ Ioinit (); /* Initialize lowio */
 
_ Acmdln = ( Char   * ) Getcommandlinea (); /* Obtain Command Line Information */
_ Aenvptr = ( Char   * ) _ Crtgetenvironmentstringsa (); /* Obtain Environment Information */
_ Setargv (); /* SET command line parameters */
_ Setenvp (); /* Set Environment Parameters */
_ Cinit (); /* C Data initialization: the initialization of global variables is 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. Only in this way can our C Language Runtime libraries and applications work normally. The program you write can run without the math library, but cannot handle complex mathematical operations. However, without the C run-time library, main () won't be called, exit () cannot be responded. 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. Runtime Library of Visual C ++ Source code It is not installed by default. If you want to view its source code, you need to reinstall visual c ++ and select the option to install the source code of the Runtime Library.
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.