HOWTO: Link with the Correct C Run-Time (CRT) LibraryLast reviewed: October 7, 1997Article ID: Q140584 |
The information in this article applies to:
SUMMARYThere are six types of reusable libraries: Static Single Threaded Library (Debug/Release), Static Multithreaded Library (Debug/Release), and Dynamic Link Library (DLL)(Debug/Release). The DLL is multithread-afe and a single- thread version of the CRT library is not provided for DLLs. If the reusable library or any user of the library is using multiple threads, then the library needs to be a multithread-safe library type. NOTE: Debug libraries and compiler switches /MLd, /MTd, and /MDd are only available in Visual C++ versions 4.0 and above. The following table shows which compiler switch should be used for building each of the six types of reusable libraries (all DLL types are multithread- safe). Any project using the reusable library should use the same compiler switch. When using the /ML(default), /MLd, /MT, /MTd, /MD, or /MDd compiler switches, the compiler places the default library name (listed under the Library column) in the object file.
Reusable Library Switch Library Macro(s) Defined Single Threaded /ML LIBC (none) Static MultiThread /MT LIBCMT _MT Dynamic Link (DLL) /MD MSVCRT _MT and _DLL Debug Single Threaded /MLd LIBCD _DEBUG Debug Static MultiThread /MTd LIBCMTD _DEBUG and _MT Debug Dynamic Link (DLL) /MDd MSVCRTD _DEBUG, _MT, and _DLLYou can view an object module to determine which switch was used when it was built by using this command:
dumpbin /all <object>.objLook in the section titled RAW DATA #1. In the right-most column, the default libraries will be listed.
MORE INFORMATIONA reusable library and all of its users should use the same CRT library types and therefore the same compiler switch. The macros defined (or not defined) for each of the compiler switches can be used in the header file(s) of your reusable library to enforce the proper compiler switch. The sample code in this article demonstrates how to use these macros. If you would like users of the library to be able to choose static or DLL CRT, you should provide both static and DLL reusable library types. If you do choose to mix CRT libraries, remember that you have two separate copies of the CRT, with separate and distinct states, so you must be careful about what you try to do across a CRT-boundary. There are many ways to get into trouble with two CRTs. Here are just a few:
Sample CodeThe following code can be used in the reusable library's header file to ensure the consistent use of the correct compiler switch:
// MyReusableStaticSingleThreadReleaseLibrary.h #if defined(_MT) || defined(_DEBUG) #error The /ML compiler switch is required. #endif // MyReusableStaticMultithreadReleaseLibrary.h #if !defined(_MT) || defined(_DLL) || defined(_DEBUG) #error The /MT compiler switch is required. #endif // MyReusableDynamicLinkReleaseLibrary.h #if !defined(_MT) || !defined(_DLL) || defined(_DEBUG) #error The /MD compiler switch is required. #endif // MyReusableStaticSingleThreadDebugLibrary.h #if defined(_MT) || !defined(_DEBUG) #error The /MLd compiler switch is required. #endif // MyReusableStaticMultithreadDebugLibrary.h #if !defined(_MT) || defined(_DLL) || !defined(_DEBUG) #error The /MTd compiler switch is required. #endif // MyReusableDynamicLinkDebugLibrary.h #if !defined(_MT) || !defined(_DLL) || !defined(_DEBUG) #error The /MDd compiler switch is required. #endif Keywords : CRTIss Version : WINNT:2.0,2.1,2.2,4.0,5.0; Platform : NT WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |