CRT originally refers to the C Runtime Library (C Runtime Library) developed by Microsoft for development and operation of the operating system. 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 ++ 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; the C ++ standard library we use in VC, it is actually a real subset of CRT (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 ++ 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.
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.
Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------
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.
Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------
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 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.
Just as the C ++ compiler is used to compile the compiler itself written in C ++, 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 ). 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 ++ !!
Generally, any operating system written in C implements a subset of CRT in the kernel, which implements operations required by the kernel and does not depend on any other libraries, there will be another CRT implementation. In this operating system, some functions are implemented without using the APIS provided by the operation, such as the pure memory operation function strncmp and others, use the API provided by the operating system. Of course, it does not work if you want to use it. For example, if you do not use the Windows API, how does it output the string to the console window? ~ Haha. In Linux, the subset of the CRT used by this operating system is called klibc and ntcrt in Windows. The complete implementation of the operating system is glibc in Linux, in Windows, it is called msvcrt.
Appendix: Some CRT function lists
[Thank you for your reference]
1. CRT and windows