The Runtime Library is Program The library files required during runtime. Generally, the Runtime Library is provided in the form of LIB or DLL. The C Runtime Library was born in 1970s. At that time, the program world was still very simple. Applications were all single-threaded, and the multi-task or multi-thread mechanism was still a new concept at this time. 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) A global variable similar to errno is set for each thread;
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. See the following table:
C Runtime Library File
Single thread (static link) libc. Lib
Debug single thread (static link) libcd. Lib
Multithread (static link) libcmt. Lib
Debug multithread (static link) libcmtd. Lib
Multithread (Dynamic Link) msvert. Lib
Debug multithread (Dynamic Link) msvertd. Lib
2. Role of C Runtime Library
In addition to providing necessary library function calls (such as memcpy, printf, and malloc), the C Runtime Library provides the most important function of adding 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.CodeAfter the author's arrangement and simplification:
Void maincrtstartup (void)
{
Int mainret;
/* Obtain the complete Win32 version information */
_ Osver = getversion ();
_ Winminor = (_ osver> 8) & 0x00ff;
_ Winmajor = _ osver & 0x00ff;
_ Winver = (_ winmajor <8) + _ winminor;
_ Osver = (_ osver> 16) & 0x00ffff;
_ Ioinit ();/* initialize lowio */
/* Obtain command line information */
_ Acmdln = (char *) getcommandlinea ();
/* Obtain environment information */
_ Aenvptr = (char *) _ crtgetenvironmentstringsa ();
_ Setargv ();/* set command line parameters */
_ Setenvp ();/* Set environment parameters */
_ 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. Only in this way can our C Language Runtime libraries and applications 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.
Runtime Library of Visual C ++Source codeIt 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.
3. Differences between various C Runtime Libraries
(1) Single-threaded library with static links
The single-threaded library with static links can only be used for single-threaded applications. The target code of the C Runtime Library is eventually compiled in the binary file of the application. You can use the/ml compilation option to set a single-threaded library that uses static links for Visual C ++.
(2) multi-thread library with static links
The target code of the multi-threaded library with static links is finally compiled into the binary file of the application, but it can be used in a multi-threaded program. You can use the/MD compilation option to set a single-threaded library that uses static links for Visual C ++.
(3) dynamically linked Runtime Library
The dynamic link Runtime Library saves all c-library functions in a separate dynamic link library msvcrtxx. dll. msvcrtxx. dll handles multithreading issues. You can use the/ml compilation option to set the Runtime Library that uses dynamic links in Visual C ++.
The/MDD,/MLD, or/MTD options use the debug Runtime Library (the runtime function library of the debug version), which corresponds to/MD,/ml, or/MT respectively. The Runtime Library of the debug version contains debugging information and uses some protection mechanisms to help identify errors and enhance the detection of errors. Therefore, it cannot match the release version in terms of running performance.
The following shows a console program that uses the C Runtime Library incorrectly:
# Include <stdio. h>
# Include <afx. h>
Int main ()
{
Cfile file;
Cstring STR ("I Love You ");
Try
{
File. Open ("file. dat", cfile: modewrite | cfile: modecreate );
}
Catch (cfileexception, E)
{
# Ifdef _ debug
Afxdump <"file cocould not be opened" <e-> m_cause <"/N ";
# Endif
}
End_catch
File. Write (STR, str. getlength ());
File. Close ();
}
The Link error occurred during "rebuild all:
Nafxcwd. Lib (thrdcore. OBJ): Error lnk2001: unresolved external symbol _ endthreadex
Nafxcwd. Lib (thrdcore. OBJ): Error lnk2001: unresolved external symbol _ beginthreadex
Main.exe: Fatal error lnk1120: 2 unresolved externals
Error executing cl.exe.
The cause of the error is that visual C ++ uses a single-threaded static Link Library for the console program by default, while the cfile class in MFC has hidden multiple threads. In Visual C ++ 6.0, choose Project> Settings> C/C ++ menus and options, and modify the compilation options in project options.
from: http://hi.baidu.com/zkbucciarati/blog/item/6b28d22b2760c6fbe6cd40fe.html