Analysis of C Runtime Library (lib dll) in Visual C ++)

Source: Internet
Author: User
Tags vc runtime

Analysis of C Runtime Library (lib dll) in Visual C ++)

I. History

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 very small (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 ).

Ii. Development

The C Runtime Library was born with a single thread. With the development of the operating system multithreading technology, the initial C Runtime Library could not meet the needs of the program.

In the C ++ world (C ++ is the superset of C), there is another concept: standard C ++ Library (the Standard C ++ Library added by VC for C ++ mainly includes: libc. lib, libcmt. lib and msvcrt. lib .), It includes the C run-time library and STL mentioned above.

In Windows, Visual C ++ provides two versions of C Runtime Library: one version for single-threaded applications to call, and the other version for multi-threaded applications to call.

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.

Iii. Functions

The Runtime Library is the library file required by the program during runtime. It is usually provided in the form of LIB or dll:

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) msvcrt. Lib
Debug multithread (Dynamic Link) msvcrtd. Lib

The C Runtime Library contains the most basic and commonly used functions for running C Programs.

In addition to providing necessary library function calls, the C Runtime Library provides another important function: to add a startup function to an 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.

Iv. compiler link options

The editor name of the vcpus is cl.exe. It has several options related to the standard library:/ml,/MLD,/mt,/MTD,/MD,/MDD. Which C Runtime Library to join the program during compilation depends on these compilation options. The options tell the compiler application the version of the C standard library to be used:

/Ml corresponds to the standard library (libc. Lib) of the single-thread static version );
/MT corresponds to the multi-threaded static version standard library (libcmt. Lib). At this time, the compiler automatically defines the _ Mt macro;
/MD corresponds to the multi-threaded dll version (imported to msvcrt. Lib, And the DLL is msvcrt. dll). The Compiler automatically defines two macros: _ MT and _ DLL.

The option D will allow the compiler to automatically define one more _ debug macro, indicating that the debugging version of the corresponding standard library will be used. Therefore:

/MLD corresponds to the single-thread static standard library (libcd. Lib) of the debug version );
/MTD corresponds to the debugging version of the multi-threaded static standard library (libcmtd. Lib );
/MDD corresponds to the debugging version of the multi-threaded DLL standard library (imported into msvcrtd. Lib, DLL is msvcrtd. dll ).

That is:

/Ml use libc. lib to create a single-thread Executable File
/MLD use libcd. lib to create and debug single-thread executable files
/MT uses libcmt. lib to create multi-thread executable files
/MTD use libcmtd. lib to create and debug multi-thread executable files
/MD use msvcrt. lib to create multi-thread DLL
/MDD use msvcrtd. lib to create and debug multi-thread DLL

Note:

(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/MT compilation option to set a multi-threaded library that uses static links in 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/MD compilation option to set the Runtime Library that uses dynamic links in Visual C ++.

The/MLD,/MTD, or/MDD options use the debug Runtime Library (the runtime function library of the debug version), which corresponds to/ml,/mt, or/MD 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.

Most of the time a program is running in these runtime libraries. When a program (release version) is compiled, VC Automatically releases the corresponding Runtime Library file (libc) based on the Compilation options (single thread, multi-thread, or DLL. lib, libcmt. lib or import library msvcrt. lib.

Note: Modify the compilation option and change/MD or/MDD to/Mt or/MTD to implement static links to the VC Runtime Library, the vc dll is no longer needed at runtime.

V. Appendix:

The following describes the Visual C ++ compiler options in msdn:

These options select a single-threaded or multi-threaded runtime routine to indicate whether the multi-threaded module is a DLL, and select the release or debug version of The Runtime Library.

/MD

Make the application use the multi-thread of the Runtime Library and the DLL-specific version. Define _ MT and _ DLL, and make the compiler put the library name msvcrt. Lib into the. OBJ file.
The application compiled with this option is statically linked to msvcrt. Lib. This library provides a code layer that allows the linker to parse external references. The actual working code is included in msvcr80.dll. This library must be available for applications linked to msvcrt. Lib at runtime.
When _ static_cpplib (/d_static_cpplib) is defined and/MD is used, it will lead to applications and static multi-threaded Standard C ++ Library (libcpmt. lib) instead of the dynamic version (msvcprt. lib) Link, still through msvcrt. lib is dynamically linked to the master CRT.

/MDD

Define _ debug, _ Mt, and _ DLL, and enable the application to use the Runtime Library to debug multiple threads and be specific to the dll version. It also enables the compiler to put the library name msvcrtd. Lib into the. OBJ file.

/Ml

Enable the compiler to put the library name libc. Lib into the. OBJ file so that the linker can use libc. lib to parse external symbols. This is the default operation of the compiler. Libc. Lib does not support multithreading.

/MLD

Define _ debug and make the compiler put the library name libcd. Lib into the. OBJ file so that the linker can use libcd. lib to parse external symbols. Libcd. Lib does not support multithreading.

/Mt

Make the application use the multi-threaded static version of The Runtime Library. Define _ MT and make the compiler put the library name libcmt. Lib into the. OBJ file so that the linker can use libcmt. lib to parse external symbols.

/MTD

Define _ debug and _ Mt. This option also enables the compiler to put the library name libcmtd. Lib into the. OBJ file so that the linker can use libcmtd. lib to parse external symbols.

/LD

Create a DLL.
Pass the/DLL option to the linker. The linker searches for the dllmain function, but does not need this function. If the dllmain function is not compiled, the linker inserts the dllmain function that returns true.
Link DLL startup code.
If the Export (. exp) file is not specified on the command line, create the import/export (. Lib); link the import/export link to the application that calls your DLL.
Convert/.
Unless explicitly specified/MD, it indicates/mt.

/LDD

Create a debug DLL. Define _ MT and _ debug.

Warning

Do not mix the static and dynamic versions of the Runtime Library. There are multiple runtime database copies in a process, which may cause problems because the static data in the copies is not shared with other copies. The linker does not stop using both static and dynamic version links in the. exe file, but you can still use two (or more) Copies of the Runtime Library. For example, when it is used together with the. exe file linked to the Runtime Library of the dll1_runtime, dynamic linked libraries connected to the Runtime Library of the static (non-DLL) version may cause problems. (You should also avoid mixing the debug and non-DEBUG Versions of these libraries in a process ).

Note:/ml,/MLD does not seem to be provided; in addition, libc. lib, libcmt. lib and msvcrt. lib has the corresponding libcp. lib, libcpmt. lib and msvcprt. lib. The function is unclear. I do not know whether to upgrade or replace the version or supplement the version or other functions -_-!... Oy ~ Orz
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.