Stats

A Short Story about VC++ CRT

The characteristics of each CRT (ML, MT, MD) and the way to solve problems begginers encounter frequently

What is CRT?

CRT is an acronym for C Runtime library. It's a huge library containing all of the C standard functions. All of the functions -- such as printf, scanf, fgets and so on -- that you first used when you learned the C language are included in this library. Basically, Visual C++ has three kinds of CRT. Those are single-threaded (ML), multi-threaded (MT) and multi-threaded DLL (MD). Each of these CRTs has two versions called debug and release, so there are six CRT libraries strictly in VC++.

CRT Characteristics

The reason why they make as many as six libraries containing the same functions is akin to Baskin Robbins', which has more than 30 kinds of
ice-cream. The idea of it is that you select the most appropriate library for your conditions. Like all the
ice-cream has different tastes, the libraries have little differences. Table 1 shows you the characteristics of each CRT. The debug CRT has some additional features like assert alert, memory leak detection and so on.

Table 1: CRT Characteristics

CRT

Library

Characteristics

single-threaded

libc.liblibcd.lib

It's made only for a single-threaded program and is a little faster than the multi-threaded CRT. Some functions included in this CRT don't run properly in the multi-threaded version. Microsoft hasn't supported CRTs of this type since Visual Studio 2005.

multi-threaded

libcmt.liblibcmtd.lib

All the functions in this CRT are safe in the multi-threaded environment.

multi-threaded DLL

msvcrt.libmsvcrtd.lib

Same as the multi-thread CRT, but CRT functions are in the separated DLL. As a result, there are many modules using this CRT and the total size of the program is reduced. The disadvantage of this CRT is the necessity of distributing the program with the CRT DLL.

How to Select CRT

If your program has no library, you just select the CRT following its character. The way to change CRTs is simple. First, open the project property dialogue and then select code generation in the C/C++ tab (See Figure 1). Now you can see the Runtime library field. Change that field to your CRT and then rebuild your project. That's all.

Figure 1: Runtime Library in the Project Property Dialogue

However, if there is a static library or dynamic link library, the choice is not so simple. First of all, you must know the binding time of those libraries. The static library is bound at compile time, but the dynamic link library is bound at run time. Both of them are ultimately executed in the same memory space of your program.

The most common problem with linking the static library is CRT collision, when the program links with a different CRT than the library uses. Since each CRT has the same functions, they can't be linked at the same time. There are two solutions for this problem. One is to recompile all libraries with the same CRT that the program uses. The other is to add a collision CRT to ignore the library field. You can find it in your project property dialogue (See Figure 2).

Figure 2: Ignore Specific Library in the Project Property Dialogue

The major problem with the dynamic link library is the new/delete issue. If your program and DLL link with a multi-threaded or single-threaded CRT, your program and DLL will use different heaps. Thus, it makes a run time error to free the DLL, allocating memory in the program or the reverse (See List 1 and List 2). You should use a multi-threaded DLL CRT or not use new/delete beyond the DLL boundaries to resolve this problem.

Share

About the Author

YoungJin is a co-founder of Wellbia.com Co., Ltd., a security company in South Korea, and Visual C++ Microsoft Most Valuable Professional. He has developed anti-cheat program called XIGNCODE since 2007. He wrote several PC security programs like PC Firewall, Anti-Spyware, and Keyboard Security Software. He has contributed a number of articles about Windows programming to Microsoftware, the famous programming magazine of South Korea. He also hosts a blog (http://www.jiniya.net) that includes articles about system programming on Windows.

Comments and Discussions

The article is not so bad for a beginner: it gives some useful informations and motivations, but I feel the title is msileading.
Although contestualized in the VSC++ environment, the subject is not C++ istelf but the CRT.

Consider someone seeking for this kind of informations the next year: a title like "A stoty about C++ runtime libraries is probably more helpful...