Information about the VC Runtime Library

Source: Internet
Author: User
Tags vc runtime


VC project properties → configuration properties → C/C ++ → code generation → runtime libraries can adopt the following methods: multi-thread (/mt) and multi-thread debugging (/MTD) multi-thread DLL (/MD), multi-thread debugging DLL (/MDD ),Single-thread (/ml), single-thread debugging (/MLD).

Reusable library Switch Library Macro (s) Defined
Single threaded /Ml Libc (None)
Static multithread /Mt Libcmt _ Mt
Dynamic Link (DLL) /MD Msvcrt _ MT and _ DLL
Debug single threaded /MLD Libcd _ Debug
Debug static multithread /MTD Libcmtd _ Debug and _ Mt
Debug Dynamic Link (DLL) /MDD Msvcrtd _ Debug, _ Mt, and _ DLL

Among them, the option ending with lowercase "D" indicates the debug version, and the option not "D" indicates the release version. In large projects, the runtime libraries of all components and third-party libraries must be unified. Otherwise, lnk2005 blowout may occur.

The single-thread Runtime Library options/ml and/MLD were scrapped after vs2003.

/MT and/MTD indicate the static lib version of the multi-threaded CRT library. This option fully embeds the Runtime Library in the form of static lib during compilation. The executable file generated by this option does not need to participate in the Runtime Library DLL, and will have a slight performance improvement, however, the generated binary code is very bloated because it is chained into a large Runtime Library. When a project is embedded into multiple projects as a static Linked database, the memory management of the runtime database may exist in multiple copies, this will eventually cause a fatal "Invalid Address specified to rtlvalidateheap" problem. In addition, the/MT and/MTD options are no longer supported in C ++ and CLI hosting.

/MD and/MDD indicate the dynamic dll version of the multi-threaded CRT library, which will make the application use the multi-threaded DLL of a specific version of the Runtime Library. When linking, The Runtime Library msvcrxx will follow the traditional VC link DLL method. DLL import to msvcrt. the LIB link requires that the corresponding version of the VC Runtime Library be installed to reissue the component package (of course, it is also possible to put these Runtime Library DLL under the application directory ). Because the/MD and/MDD methods do not link the Runtime Library to the executable file, the size of the executable file can be effectively reduced. When multiple projects run in the MD mode, the same heap is used internally, and memory management is simplified, so that the problem of cross-module memory management can be mitigated.

Conclusion:/MD and/MDD will be the trend,/ml and/MLD methods should be abandoned in time,/MT and/MTD should not be used unless necessary.

 

Refer:

How to link with the correct C run-time (CRT) Library

/MD,/mt,/LD (use the Runtime Library)

2011-12-26

C ++/CLI if multiple projects "code generation/Runtime Library" are inconsistent, the lnk2022 error may be reported.

Reference: Getting error when compiling debug mode: C ++/cli-error lnk2022

 

 

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) 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.

C Runtime LibraryThe main function of the Start function is to initialize the program, assign an initial value to the global variable, and load the entry function of the user program..

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.

**************************************** *******
Http://dev.cbw.com/c/c/2005965601_4211746.shtml

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 with Microsoft. NET
1.0 (for VB. NET and C #) Library. 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.

 

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.