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

Source: Internet
Author: User

First of all, I have been Google for a long time and want to find the English document about it, but I can't find it, so I have to repost the discussion from Chinese people.

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 ++ compiled with VC Program They 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.
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.
About ATL
ATL is built on CRT. If you read the source code of ATL, you will know. The link is not required because the ATL library is statically linked to the CRT, so it can run outside the CRT. A misunderstanding like this is that it obfuscated the CRT that serves as the underlying basic library and the CRT that is attached to the VC as a product. Although the two are the same code, the concept is different.
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 CRT Source code It 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 ++!

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

C Runtime Library

This is too confusing for a long time. Now I want to paste one

Copied to learn together
***************************************
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.

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.