C Runtime Library [ZT]

Source: Internet
Author: User
Source Address: http://www.cnblogs.com/chio/archive/2007/11/26/972152.html

C Runtime Library

CRT (in a Windows platform in a narrow sense) originally refers to the C Runtime library developed by Microsoft for the development and operation of Windows operating systems. Later, C ++ Runtime Library was developed based on Microsoft. 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 ++ compiled with VCProgramThey are also used (they can be dynamically linked or statically linked. when running the former, you need to install the crt dll in the system, and the latter does not need it ). 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 (different platforms have their own implementations of the C ++ standard library ); the C ++ standard library we use in VC is actually a real subset of CRT (less than the one not included in the C ++ standard ).Code, Especially a large number of low-level C code ).

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.

The C ++ standard is a general language specification for C ++. It guides all c ++ users (and implementers ?). 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 standards in the standardization process) (first-class enterprise production standards, second-stream enterprise production technology, and third-stream enterprise production products ). 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.

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.

In addition to the above introduction, you also need to understand the following when using CRT:

1. Some components of the 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.

2. Most of the content of CRT is cross-hardware platform, but there are also some parts, is directly written in Assembly, based on the hardware platform, and optimized based on a specific hardware platform (rather than giving the compiler the responsibility for generating machine code ). For example, the x32 of InDel was optimized in the early days, and now the optimization of amd64 is not implemented across hardware platforms.

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, so the CRTSource codeIt 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 the unique programming features on the Windows platform, such as thread. (on the Linux platform, there is no such explicit process or thread concept as on Windows ). 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 (if necessary, you can compile the CRT code in the above VC Directory into modules on other platforms, so you should be able to become a CRT product on other platforms ). 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, and does not include the code used to generate them (the Code put in the CRT/src directory of VC is the essence of CRT ).

Just like the C ++ compiler used to compile the compiler itself written in C ++ (this sentence is a bit difficult. For example, assume that VC is a C ++ compiler, in addition, I wrote some code in C ++. The function of these code is to implement another compiler called VB, which is used to compile basi.C Language.Then, we can use vcss to compile the code to generate a vb.exe compiler. Windows (and the compiler on it) is used as a platform to develop and compile the CRT, it also uses CRT to write Windows itself (of course, the first CRT and the first compiler used to compile Windows are not developed on Windows) (like Windows 2000 to Windows XP, there should be no major change in the essence of the CRT, because the main versions of the two are the same: 5, so it is to use the CRT on Win2k to develop XP ?).

Just like "I", you can also write a class library, then write an operating system based on it, expand the class library on this operating system, and then release it with the compiler, develop some supporters of my operating system, and earn more income by the way. Or release another database in another mode (just a product I developed on the original database, Because I independently released this new database, many people will not know the relationship between the new database and the old database. This is good, because programming itself is to hide details as much as possible and be transparent to users as much as possible) to attract developers of different styles. In this way, I got the biggest reward-because I didn't publish the source code of the operating system, many users think that I have not only implemented the system, but also made the compiler, but also developed a class library. If so many things are done, the return should be. In fact, they do not know that the class library is required for compiling the operating system, and the compiler is required. These necessary things can get more returns outside the operating system, which is perfect! What is this? This is the business spirit! Of course, these misunderstandings are good for me, so I don't have to preach the truth everywhere. I have released all the source code of the class library. I just added something related to my operating system to the original class library to help people who write programs on my system. This is my kind of kindness; as for the possibility of further misunderstanding, it is not something I need to consider ......

So let's take a look at the source code of CRT-look at the compilation of the hardware platform; look at the Standard C ++ library of VC and the relationship between CRT; then look at the source code of other operating systems, think about what part of the CRT can be used to write the operating system, and what else do I need if I write the system myself? You can even look at the DOS source code and think about the similarities with the CRT, and historical origins. Unfortunately, you cannot see the source code of windows. Otherwise, Everything will be clear.

Finally, C ++ is certainly not Microsoft's patent. However, Microsoft chose C ++ and succeeded. This is for sure: Like CRT, Vc, windows, office, and sqlserver ...... 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 ++!

-------------------------------------------

C Runtime Library

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 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.
 
3) subsequently, with the prevalence of C language, the C compiler manufacturers, individuals, and 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 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 ).

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 and static runtime libraries.
the dynamic Runtime Library is mainly the dll library file msvcrt. DLL (or msvcrtd. DLL for debug build), 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)
you can use the following method in VC 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 one function, select the Runtime library function, the vendor of each C compiler provides unified support for the Standard C run-time library.

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.