[Reprinted] C Runtime Library (msvcrt) History

Source: Internet
Author: User
Tags microsoft c
(Reprinted) origins of C Runtime Library (msvcrt)

Msvcrt. DLL (Name: Microsoft C Runtime Library) provides C language library functions such as printf, malloc, strcpy, and uses C/C ++ (VC) the compile program provides initialization (such as obtaining command line parameters) and exit functions.

 

C Runtime library history, API, STL, MFC, and ATL relationships

When I first got started with Windows development, I checked a lot of information for quick start. I did not expect that the more I read, the more confused my head. Because programs that write the same function have different routines. For C Programs, Some APIs have long names and are all unknown variable types in uppercase; some are similar to C development in a familiar Linux environment, and the standard short C function names. For example, some C ++ programs use different standard classes. Later, it took a little effort to understand that the development on the Windows platform had multiple different systems: Standard C, Windows API, MFC, and STL. These are pre-compiled libraries. The actual development depends on the project size and nature, program style, and personal preferences.

 

I. C/C ++ in the free world

1. CRT

The Runtime Library is the c run-time library, which is a concept of C rather than the C ++ language world. This is because the functions in these libraries are required when your C program runs.

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 used C to rewrite more than 90% of UNIX system functions, and separated the most commonly used functions to form header files and corresponding libraries, c run-time library is formed in this way.

Subsequently, with the prevalence of C language, various C compiler manufacturers/individuals/groups 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 defines in detail the specific meaning of each element of the C language and the implementation requirements of the compiler, introduces a new function declaration method, and sets up the standard form of the 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 ).

C run-time library contains initialization code and error handling code (such as divide by zero ). The program you write can run without the math library, but cannot handle complex mathematical operations. However, without the C run-time library, main () won't be called, exit () cannot be responded. Because the C run-time library contains the most basic and commonly used functions for C program running.

 

2. Standard C ++ Library

In the C ++ world, there is another concept: Standard C ++ library, which includes the C run-time library and STL mentioned above. The reason for including the C run-time library is obvious. c ++ is the superset of C, and there is no reason to re-create a C ++ run-time library. The standard C ++ Library added by VC for C ++ mainly includes libcp. Lib, libcpmt. lib and msvcprt. Lib.

 

2. Microsoft C/C ++

1. CRT in Windows

CRT originally refers to the C Runtime library developed by Microsoft and used for operating system development and operation. Later, C ++ Runtime Library was developed on this basis. Therefore, CRT is the C/C ++ Runtime library developed by Microsoft. In the CRT/src directory of VC, you can see the source code of CRT, which includes both C and C ++.

The original goal of CRT is to support the operation of the operating system. In addition to the assembly part, Windows operating systems are all compiled in C/C ++. Therefore, the kernel and many key services are run on the CRT (they all use dynamic links of DLL technology ). In addition, C/C ++ programs written in VC also use them (they can be dynamically linked or statically linked. The former requires a crt dll installed in the system during running, the latter does not need ). It can be said that CRT is the low-level class library used by Microsoft to compile Windows. Then, it is included in the VC series as an implementation of the C ++ standard library; the C ++ standard library we use in VC, actually, it is a real subset of CRT (less code not included in the C ++ standard, especially a large number of low-level C code)

The C ++ standard is a general language specification for C ++ and guides all c ++ users. One part of CRT can be seen as a C ++ standard library developed by Microsoft (in fact, this is also true when Microsoft develops CRT, refer to the C ++ language specification in the standardization process ). It has a certain gap with the c ++ standard, partly because CRT has been developed and put into use before C ++ has completed standardization. To be backward compatible with previous windows code, there is always a gap between the early CRT and C ++ standards. However, the CRT is constantly being improved. There is still a big gap between the CRT and the C ++ standard in the vc6 band, while the vc8 almost fully complies with the c ++ standard.

 

2. CRT and Windows API in Windows

As for the relationship between CRT and Windows API, Windows API, as part of windows, is developed on the basis of CRT. You can think of Windows (and its APIs) as a project. The language of this project is assembly/C ++, and the class library used is CRT. Therefore, Windows APIs cannot be used without CRT.

When writing an operating system, you need an appropriate low-level library to complete some basic and repeated work. So there is a CRT. At the lowest layer, there is no such concept as DLL at all, so the source code of CRT can only be made into lib and is statically linked. Then, as windows becomes more and more complex, Microsoft puts forward the concept of API, which provides a set of interfaces for Windows developers to operate windows directly. This is the Windows API. Of course, Windows APIs are also written on CRT.

Next, Microsoft wants to provide C/C ++ programmers with sufficient support. In addition to the original CRT, it also needs to add something special to programming on Windows platforms, such as thread. These things are platform-related and can only be built on Windows APIs. These new content is also put into the CRT. In this case, the CRT not only contains code unrelated to the platform at the lowest layer, but also platform-related code. For example, if you call CRT's _ beginthread, createthread of Windows API is actually called internally. After adding these things, CRT is still used to write the operating system; but obviously, those that call the Windows API have lost the value shifting.

Then, CRT is encapsulated into a product and released along with the compiler. At this time, the lib and DLL of the CRT product are in Windows format. You cannot use EXE or DLL on platforms other than windows. This is the difference between the CRT and CRT products. Windows API products, or many other components of windows, are also some lib/DLL files. These are superficial things and are bound with windows. However, if you think that you have a CRT only when you have a Windows or Windows API first, you will put it upside down. Unless your definition of CRT is those lib/DLL products, it does not include the code used to generate them.

Of course, some components of CRT also call the Windows API. This may be why some people think that CRT is based on the Windows API. But in fact, there is no problem with this part of the stripped CRT. Microsoft only adds the C/C ++ low-level libraries that can be used on the Windows platform to the CRT. Therefore, a large part of CRT is unrelated to the operating system platform (the original CRT) and is the basis for developing Windows itself and everything on it. They can also be used as a C/C ++ library on other operating system platforms. Another part is closely bound to Windows. Calling Windows APIs can be considered as an extended CRT. These two parts are put together because they are all required for developing Windows operating systems, and also because they are required by C/C ++ programmers on Windows platforms. This complex relationship is caused by Microsoft's human factors. Therefore, CRT cannot be considered based on windows or Windows APIs.

In summary, a real subset of CRT (Microsoft's C/C ++ Runtime Library) (mainly C ++ Runtime Library) is a conformity (or at least an attempt) c ++ Standard C ++ library. Windows APIs (and many other parts of Windows) are developed on the basis of CRT.

Finally, C ++ is certainly not Microsoft's patent. However, Microsoft chose C ++ and succeeded. This is for sure: Like CRT, Vc, windows, office, and SQL Server. This demonstrates the advantages of C ++ and Microsoft's own factors. Then, of course, it must grasp the C ++ flag, vigorously promote its own C ++, and reject other C ++. This is the "style" of the Empire. So for Microsoft, I always hate and love Microsoft. I always hope that it will find it with my conscience someday. Of course, this is just an illusion. However, it must be certain. It should always be true if it is no longer true. However, Microsoft is not the best product, but most of them are the most successful. When you see its shortcomings, you must also see its advantages. Even if it is not reasonable, it must be reasonable. Therefore, Microsoft and its success cannot be evaluated in one or two sentences. Only one thing is certain. It is wise to choose C ++!

========================================================== ========================================================== ========================

1) The Runtime Library is the c run-time library, which is the concept of the C language rather than the C ++ language World: The name is because the functions in these libraries are required when your C program is running.

 
2) C is a so-called "Small kernel" language, which is small in itself (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 the corresponding library are formed in this way in C run-time library.

 
3) subsequently, with the prevalence of the C language, the C compiler manufacturers/individuals/groups 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 defines in detail the specific meaning of each element of the C language and the implementation requirements of the compiler, introduces a new function declaration method, and sets up the standard form of the 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 ).

4) C run-time library contains initialization code and error handling code (such as divide by zero ). The program you write can run without the math library, but cannot handle complex mathematical operations. However, without the C run-time library, main () won't be called, exit () cannot be responded. Because the C run-time library contains the most basic and commonly used functions for C program running.

5) in the C ++ world, there is another concept: Standard C ++ library, which includes the C run-time library and STL mentioned above. The reason for including C run-time library is obvious. c ++ is the superset of C, and there is no reason to re-create a C ++ run-time library. the standard C ++ Library added by VC for C ++ mainly includes libcp. lib, libcpmt. lib and msvcprt. lib

6) in windows, the C run-time library provided by VC is divided into dynamic Runtime Library and static Runtime Library.
The dynamic Runtime Library is mainly the dll library file msvcrt. dll (or msvcrtd. DLL for debug build), and the corresponding import library file is msvcrt. Lib (msvcrtd. Lib for debug build)

The main files corresponding to the static Runtime Library (release version) are:
Libc. Lib (single thread static library, retail version)
Libcmt. Lib (multithread static library, retail version)
Msvcrt. dll provides thousands of C functions. Even low-level functions such as printf are in msvcrt. dll. In fact, most of the time your program runs in these runtime libraries. When your program (release version) is compiled, VC Automatically releases the corresponding Runtime Library file (libc) based on your compilation options (single thread, multi-thread, or DLL. lib, libcmt. lib or import library msvcrt. lib.

Which C run-time library is connected to your program during compilation depends on the Compilation options:
/MD,/ml,/mt,/LD (use run-time Library)

In VC, you can use the following method to set which C run-time library to join your program:
To find these options in the development environment, click Settings on the project menu. then click the C/C ++ tab, and click code generation in the category box. see the use run-time library drop-down box.
Considering program portability, if both functions can complete a function, select the Runtime library function, because the C compiler manufacturers provide unified support for the Standard C 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.