Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C

A Short Story about VC++ CRT

3.15/5 (22 votes)
28 Aug 2007CPOL3 min read 3  
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

CRTLibraryCharacteristics
single-threadedlibc.lib
libcd.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-threadedlibcmt.lib
libcmtd.lib
All the functions in this CRT are safe in the multi-threaded environment.
multi-threaded DLLmsvcrt.lib
msvcrtd.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
Screenshot - crt1.png

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
Screenshot - crt2.png

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.

List 1: Bad Code Using New/delete beyond DLL Boundaries

C++
//CreateCar is a dll function.  
Car *car = CreateCar();  
delete car; 

List 2: The Code Fixed not Using New/delete Beyond DLL Boundaries

C++
//CreateCar, DestroyCar is a dll function  
Car *car = CreateCar();  
DestroyCar(car); 

Conclusion

To avoid complicated problems, the better way is to use the same CRT and write a program that does not use new/delete beyond the DLL boundaries.

History

  • 28 August, 2007 -- Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)