C Runtime Library (C run-time libraries/MD/ml/mt)

Source: Internet
Author: User

The Runtime Library is the library file required by the program 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 Library files
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) Msvcrt. Lib
Debug multithread (Dynamic Link) Msvcrtd. 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. The following code has been compiled and simplified by the author:

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.

The source code of the Runtime Library of Visual C ++ 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

A 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 into 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 multi-threaded programs. 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.

**************************************** *******

C Runtime Library in VC refers to the function library that implements the Standard C interface
Static library and dynamic library
Contains three files: libc. Lib, libcmt. Lib, and msvcrt. Lib.
Which library is used depends on your options during compilation
See the following table for details:
C run-time library features macro definition of compilation options
(Without iostream)
Libc. Lib single threaded, static link/ml
Libcmt. Lib multithreaded, static link/MT _ Mt
Msvcrt. Lib multithreaded, dynamic link/MD _ Mt, _ DLL
(Import library for msvcrt. dll)

 
 
 
Literally, the Runtime Library is the library file required by the program during running. Generally, the Runtime Library is provided in the form of DLL. The runtime libraries of Delphi and C ++ Builder are. BPL files, which are still DLL files. The Runtime Library generally includes functions frequently used for programming, such as string operations, file operations, and interfaces. The functions supported by different languages are usually different, so the databases used are also completely different. This is why there are VB runtime libraries, C Runtime libraries, and Delphi runtime libraries. Even in C ++, different libraries may be used for different functions. For example, the Runtime library used by VC ++ is completely different from the C ++ builder.
If you do not use the Runtime Library, each program will contain a lot of repeated code. Using the Runtime Library can greatly reduce the size of the compiled program. On the other hand, because the Runtime library is used, it is troublesome to include these libraries in the distribution program. If the corresponding running database program cannot be found in the operating system, it cannot be run. To solve this problem, windows always carries the latest Runtime Library of the software it has developed. Versions like Windows 2000 and later include Visual Basic 5.0/6.0 libraries. Internet Explorer always comes with the latest visual c ++ 6.0 library. Windows XP comes with a library of Microsoft. NET 1.0 (for VB. NET and C. Visual c ++, Delphi, and C ++ builder allow users to select whether the compiled program depends on the Runtime Library. However, VB, Foxpro, PowerBuilder, LabWindows/CVI, and Matlab do not allow users to make such choices and must rely on 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.