C Language Runtime Library detailed

Source: Internet
Author: User

Turn from: http://club.topsage.com/thread-541343-1-1.html

The Run-time library is the library file that the program needs at run time, usually in the form of Lib or DLL. C Run-time Library was born in the 1970s, the program world is still very simple, applications are single-threaded, multitasking or multithreaded mechanism in this time is also a new concept. So the C Run-time Library of this period is single-threaded.

With the development of multithreading technology in the 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 may cause conflicts in multithreaded programs. Suppose that all two threads set errno at the same time, and the result is that the errno will overwrite the previous one, and the user will not get the correct error message.

As a result, Visual C + + provides two versions of the C Run-time library. One version for single-threaded application calls and another version for multi-threaded applications. There are two significant differences between multithreaded run-time libraries and single-threaded Run-time libraries:

(1) A global variable similar to errno, with each thread setting one individually;

This allows the correct error message to be obtained from each thread.

(2) The data structure in the Multithread library is protected by the synchronization mechanism.

This avoids conflicts at the time of the visit.

The multithreaded Run-time libraries provided by Visual C + + are divided into static and dynamic-link libraries, and each class of run-time libraries can be subdivided into both the debug and Release editions, so Visual C + + provides 6 run-time libraries. As shown in the following table:

C Run-time 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

The function of 2.C Run-time Library

In addition to providing us with the necessary library function calls (such as memcpy, printf, malloc, etc.), the C Run-time Library provides another of the most important features for adding a startup function to an application.

The main function of the C Run-time Library startup function is to initialize the program, to assign the initial value to the global variable, and to load the entry function of the user program.

The entry point for a console program that does not take a wide character set is mainCRTStartup (void). Let's take this function as an example to analyze what kind of portal program the runtime Library has added to us. This function is defined in CRT0.C, and the following code is sorted and simplified by the author: void mainCRTStartup (void)
{
int Mainret;
/* Obtain WIN32 FULL VERSION information * *
_osver = GetVersion ();
_winminor = (_osver >> 8) & 0x00ff;
_winmajor = _osver & 0x00ff;
_winver = (_winmajor << 8) + _winminor;
_osver = (_osver >>) & 0x00ffff;

_ioinit (); /* Initialize LOWIO * *

/* GET command line information * *
_ACMDLN = (char *) getcommandlinea ();

/* Access to environmental information * *
_aenvptr = (char *) __crtgetenvironmentstringsa ();

_SETARGV (); /* SET command line Parameters/*
_SETENVP (); /* Set Environment parameters * *

_cinit (); /* C Data initialization: Global variable initialization, right here. */

__initenv = _environ;
Mainret = Main (__ARGC, __ARGV, _environ); /* Call main function * *

Exit (Mainret);
Copy code from the above code, the runtime does some initialization work before calling the main or WinMain function of the user program. After initialization is complete, we then invoke the main or WinMain function we wrote. Only in this way can our C language runtime libraries and applications work properly.

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

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

3. The difference between the various C Run-time libraries

(1) Static link of single line threading

Statically linked single-threaded threading can only be used for single-threaded applications, and the target code for the C Run-time Library is eventually compiled into the application binaries. The/ML compilation option enables you to set up a single-line threading with static links for Visual C + +.

(2) statically linked multi-line threading

The target code for the statically linked multi-line threading is also eventually compiled in the application binaries, but it can be used in multithreaded programs. The/MD compilation option enables you to set up a single-line threading with static links for Visual C + +.

(3) dynamic Link Run-time Library

The dynamic Link Run-time library saves all C library functions in a separate dynamic-link library MSVCRTxx.DLL, MSVCRTxx.DLL handles multithreaded issues. Use the/ML compilation option to set up a run-time library of Visual C + + that uses dynamic links.

The/MDd,/MLD, or/MTD options use the Debug Runtime library (the debug version of the Run-time function library) and correspond to/MD,/ml, or/MT respectively. The debug version of the Runtime Library contains debugging information and has a number of protection mechanisms to help identify errors and enhance detection of errors, so it does not match release versions in performance.

Here is a console program that does not use the C Run-time library correctly: #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 could not to be opened" << e->m_cause << "\ n";
#endif
}
End_catch

File. Write (str,str. GetLength ());
File. Close ();
Copy code we have a link error when "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 reason for the error in the replication code 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 multithreading. All we need to do is select the Project->settings->c/c++ menu and options in Visual c++6.0, and modify the compilation options in Project Options.

C Run-time Library is Microsoft's implementation of standard C library functions, because at that time, considering that many programs are written in C, and these programs are to use the standard C library, in the past, each program will eventually copy a standard library implementation into the program, At the same time in memory there may be many copies of the standard library code (one program), so Microsoft for efficiency considerations to the standard C library as a dynamic link to implement, so that multiple programs using the C standard library in memory only a copy.   (For each program, it is the equivalent of owning a copy of the global variable in the standard library, and it does not conflict with sharing the same code). This is an extension of the C standard library, as to say that the static link is still called the Run-time library that can only say that this is a habit problem.

The Run-time library is the same as a normal DLL, and is not loaded until a program is used, and no memory resides when the program is used. That said, but how many system things may also be written in C, the existence of these things so that C Run-time inventory is in memory

Literally, the runtime is the library file that the program needs at run time. Typically, the runtime is provided as a DLL. The runtime for Delphi and C + + Builder is a. bpl file, which is actually a DLL. The runtime generally includes functions commonly used in programming, such as string manipulation, file manipulation, interface, and so on. The functions supported by different languages are usually different, so the library used is completely different, which is why there are VB Run-time Library, C Run-time Library, Delphi Run-time Library of the reasons. Even if all are C + + languages, different libraries may be used because the functions provided are different. such as VC + + use of the Run-time Library and C + + Builder is completely different.
If you do not use the runtime, each program will include a lot of duplicate code, while using the runtime, you can greatly reduce the size of the compiled program. On the other hand, because the runtime is used, it is cumbersome to have these libraries in the distribution program. Cannot run if the corresponding runtime program is not found in the operating system. To resolve this contradiction, Windows always brings the latest runtime of the software that it has developed. Versions such as Windows 2000 include a library of visual Basic 5.0/6.0. Internet Explorer always has a library with the latest Visual C + + 6.0. Windows XP has a library with Microsoft. NET 1.0 (for vb.net and C #). Visual C + +, Delphi, and C + + Builder allow users to choose whether the compiled program relies on the runtime. And VB, FoxPro, PowerBuilder, LABWINDOWS/CVI and MATLAB do not allow users to make this choice, you must rely on the Run-time 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.