Click here to Skip to main content
Click here to Skip to main content
Go to top

A Short Story about VC++ CRT

, 28 Aug 2007
Rate this:
Please Sign up or sign in to vote.
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.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-threaded libcmt.lib
libcmtd.lib
All the functions in this CRT are safe in the multi-threaded environment.
multi-threaded DLL msvcrt.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

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

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

//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)

Share

About the Author

YoungJin Shin
Chief Technology Officer Wellbia.com Co., Ltd.
Korea (Republic Of) Korea (Republic Of)
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.
Follow on   Twitter

Comments and Discussions

 
GeneralCRT & API Pinmembergclu021214-Nov-07 16:30 
GeneralList 2 Pinmemberinline4-Sep-07 21:09 
QuestionIs the title consistent? Pinmemberemilio_grv28-Aug-07 19:48 
AnswerRe: Is the title consistent? Pinmembercodemaru28-Aug-07 20:35 
GeneralA problem in your List 1 Pinmemberkelcel28-Aug-07 7:16 
GeneralRe: A problem in your List 1 Pinmembercodemaru28-Aug-07 16:25 
GeneralRe: A problem in your List 1 Pinmembergiselly28-Aug-07 19:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 28 Aug 2007
Article Copyright 2007 by YoungJin Shin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid