CRT (C run-time Library) details

Source: Internet
Author: User
Tags vc runtime

A generation

The run-time library is a library file that is required by the program at run time, usually in the form of a Lib or DLL.

C Run-time libraries are C Run-time Library, born in the 1970s, is the concept of C and not the C + + language world , c programs need to run the functions of these libraries.

C language is the so-called "small kernel" language, in terms of its language itself is very small (not many keywords, program flow control, data types, etc.). So, after the C language kernel was developed, Dennis Ritchie and Brian Kernighan used C itself to rewrite more than 90% of UNIX system functions, and to separate the most commonly used parts, forming the header file and the corresponding library, This is how the C run-time library is formed.

With the popularity of C, each C compiler's producers/individuals/groups follow the old tradition and have a corresponding standard Library on different platforms, but most implementations are related to each platform. Because of the various C compiler support and understanding of C has a lot of differences and subtle differences, so there is the ANSI c;ansi C (subjective intent) detailed provisions of the C language elements of the specific meaning and compiler implementation requirements, the introduction of a new method of function declaration, and the establishment of standard The standard form of the library. so the C run-time library is provided by the compiler manufacturer . As for the header files and library functions provided by other vendors/individuals/groups, they should be called third-party C run-third (c Run-time libraries).

Two development

In the C + + world, there is another concept: the standard C + + library, which includes the C Run-time library and STL mentioned above. The reason for the inclusion of the C Run-time Library is that C + + is a superset of C, and there is no reason to re-run-time a C + + library. VC for C + + to join the standard C + + library mainly includes: LIBCP.LIB, LIBCPMT. Lib and msvcprt. Lib

C Runtime Library Just born, the program world is very simple, the application is single-threaded, multi-tasking or multi-threaded mechanism at this time is also a new concept. So the C run-time library for this period is single-threaded.

With the development of multithreading technology of operating system, the initial C run-time library can not meet the requirements of the program, and there are serious problems. The C run-time library uses multiple global variables (such as errno) and static variables, which can cause conflicts in multithreaded programs. Assuming that all two threads are set errno at the same time, the result is that the errno will overwrite the previous settings and the user will not get the correct error message.

Therefore, Visual C + + provides two versions of the C run-time library. One version is intended for single-threaded application invocation and the other for multithreaded applications. There are two significant differences between a multithreaded runtime library and a single-threaded runtime library:

(1) A global variable similar to errno, with each thread setting one individually. This allows you to get the correct error message from each thread.

(2) The data structure in multi-threaded library is protected by synchronization mechanism. This avoids the time-to-access conflicts.

The multi-threaded runtime library provided by Visual C + + is divided into static-link libraries and dynamic-link libraries, each of which can be subdivided into debug and release versions, so Visual C + + provides a total of 6 runtime libraries. The following table : (both single-threaded and multi-threaded are available in debug and release versions; only multiple threads have static-link libraries and dynamic-link libraries)

TD valign= "Top" width= "235" >multithread (dynamic Link)
c Runtime library 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
msvcrt.lib (import library)
debug multithread (dynamic link) msvcrtd.lib (import library)

The role of the three-C runtime library

(1) The C run-time library contains the most basic and commonly used functions (such as memcpy, printf, malloc, and so on) that the C program runs.
(2) In addition to providing us with the necessary library function calls, the C run-time library provides another of the most important functions for adding a startup function to an application. The main function of the C run-time library startup function is to initialize the program, assign the initial value to the global variable, and load the entry function of the user program.

The entry point for a console program that does not adopt a wide character set is mainCRTStartup (void). Let's take this function as an example to analyze what kind of entry program The runtime Library has added to us. This function is defined in CRT0.C, and the following code is compiled and simplified by the author:

void mainCRTStartup (void)
{
int Mainret;
/* Get WIN32 Full version information */
_osver = GetVersion ();
_winminor = (_osver >> 8) & 0x00FF;
_winmajor = _osver & 0x00FF;
_winver = (_winmajor << 8) + _winminor;
_osver = (_osver >>) & 0x00ffff;
_ioinit (); /* Initialize LOWIO */

_ACMDLN = (char *) getcommandlinea (); /* GET command line information */
_aenvptr = (char *) __crtgetenvironmentstringsa (); /* Get environmental information */
_SETARGV (); /* SET command-line arguments */
_SETENVP (); /* Set Environment parameters */
_cinit (); /* C Data initialization: Global variable initialization, right here! */
__initenv = _environ;
Mainret = Main (__ARGC, __ARGV, _environ); /* Call the main function */
Exit (Mainret);
}

From the above code, the runtime has done some initialization work before invoking the main or WinMain function of the user program. After initialization is complete, the main or WinMain function that we have written is then called. Only in this way can our C-language runtime libraries and applications work properly.

You can write a program without the math library, the program will run, but can not handle complex mathematical operations, but without the C Run-time Library, main () will not be called, exit () can not be responded to.

In addition to CRT0.C, the C run-time library contains wcrt0.c, WINCRT0.C, and wwincrt0.c three files to provide an initialization function. WCRT0.C is a wide-set version of CRT0.C, WINCRT0.C contains entry functions for Windows applications, and WWINCRT0.C is a wide-set version of WINCRT0.C.

The run-time library source code for Visual C + + is not installed by default. If you want to view its source code, you will need to reinstall Visual C + + and select the Install Runtime source code option when reloading.

Four different C run-time libraries

Compiler Link Options:

The C run-time library in which the Visual C + + compilation is compiled depends on the compilation options, and the options tell the compiler application what version of the C standard library to use. Options related to the standard library:/ml,/MLD,/MT,/MTd,/MD,/MDD

/ML Standard library (libc.lib) corresponding to single-threaded static version;
/MT corresponds to the multithreaded Static edition standard library (LIBCMT.lib), at which point the compiler automatically defines _MT macros;
/MD corresponds to a multithreaded DLL version (import library msvcrt.lib,dll is Msvcrt.dll), the compiler automatically defines _MT and _dll two macros.

The option followed by D will let the compiler automatically define a _DEBUG macro that will use the debug version of the corresponding standard library, so:
/MLD corresponding Debug version single-threaded static standard library (LIBCD.LIB);
/MTD corresponding Debug version of the multithreaded Static standard library (LIBCMTD.LIB);
/MDD corresponds to the debug version of the multithreaded DLL standard library (import library Msvcrtd.lib,dll is Msvcrtd.dll).

In vc2005/vc6.0, choose which C Run-time library to import into your program by setting the following method:

VC2005: To find these options in the development environment, click Project on the Project menu, choose Properties ... item. Then click the C + + tab, and click Code Generation in the Category box. See the Runtime Library drop-down box.

VC6.0: To find these options under the development environment, click Settings on the Project menu. Then click the C + + tab, and click Code Generation in the Category box. See the Use Run-time Library drop-down box.

Differences between the various C run-time libraries:

(1) Static link single-line libraries

A statically linked single-threaded libraries can only be used for single-threaded applications, and the target code of the C run-time library is eventually compiled into the application's binaries. The/ML compilation option lets you set the single-line libraries that Visual C + + uses for static linking.

(2) Statically linked multi-line libraries

The target code for statically linked multiline libraries is also eventually compiled in the application's binaries, but it can be used in multithreaded programs. The/MT compilation option lets you set the single-line libraries that Visual C + + uses for static linking.

(3) Dynamically linked run-time libraries

The dynamically linked run-time library stores all C library functions in a separate dynamic-link library MSVCRTxx.DLL, MSVCRTxx.DLL handles multithreading issues. Use the/MD compilation option to set the runtime library for Visual C + + to use dynamic linking.

The/MLD,/MTD, or/MDD options use the Debug Runtime Library (debug version of the runtime Library) to correspond to/ML,/MT, or/MD respectively. The debug version of the runtime library contains debug information, with some protection mechanisms to help detect errors, enhanced detection of errors, and therefore less operational performance than the release version.

When the program runs, a large part of the time is running in these runtimes. When the program (release version) is compiled, the VC automatically links the corresponding run-time library files (libc.lib, LIBCMT.lib, or import library MSVCRT.lib) based on the compilation options (single-threaded, multithreaded, or DLL).

Note: Modify the Compile option, change/MD or/MDd to/MT or/MTD, implement the VC runtime Library static link, at run time no longer need VC DLL. (a static link compiles the target code directly into the application's binaries, so the DLL is not needed for the runtime)

Reference

1 http://blog.chinaunix.net/u2/75914/showart_1861328.html

2 http://www.99inf.net/SoftwareDev/VC/28090.htm

CRT (C run-time Library) details

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.