How to link with the correct C Run-Time (CRT) library
- Static Single Threaded Library(Debug/Release)
- Static Multithreaded Library (Debug/Release)
- DynamicLink Library (DLL)(Debug/Release)
The DLL is multithread-safe and a single-thread version of the CRT library is not provided for DLLs. If the reusablelibrary or any user of the library is using multiple threads, then thelibrary needs to be a multithread-safe library type.
Note Debug libraries and compiler switches /MLd, /MTd, and /MDd are onlyavailable in Visual C++ versions 4.0 and later.
The following table shows which compiler switch should be used for buildingeach of the six types of reusable libraries (all DLL types are multithread-safe). Any project that uses the reusable library should use the same compilerswitch. When using the /ML(default), /MLd, /MT, /MTd, /MD, or /MDd compilerswitches, the compiler places the default library name (listed under theLibrary column) in the object file.
Reusable Library Switch Library Macro(s) Defined----------------------------------------------------------------Single Threaded /ML LIBC (none)Static MultiThread /MT LIBCMT _MTDynamic Link (DLL) /MD MSVCRT _MT and _DLLDebug Single Threaded /MLd LIBCD _DEBUGDebug Static MultiThread /MTd LIBCMTD _DEBUG and _MTDebug Dynamic Link (DLL) /MDd MSVCRTD _DEBUG, _MT, and _DLLYou can view an object module to determine which switch was used whenit was built by using this command:
dumpbin /all <object>.obj
If you would like users of the library to be able to choose static or DLLCRT, you should provide both static and DLL reusable library types.
If you do choose to mix CRT libraries, remember that you have two separatecopies of the CRT, with separate and distinct states, so you must becareful about what you try to do across a CRT-boundary. There are many waysto get into trouble with two CRTs. Here are just a few:
- There are two separate heaps. You cannot allocate (explicitly with new, malloc, or so on -- or implicitly with strdup, strstreambuf::str, or so on), and then pass the pointer across a CRT-boundary to be freed.
- You cannot pass a FILE* or file handle across a CRT-boundary and expect the "stdio low-level IO" to work.
- You cannot set the locale in one and expect the other's locale to be set.
Sample codeThe following code can be used in the reusable library's header file toensure 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
Article ID: 140584 - Last Review: 12/04/2015 12:31:20 - Revision: 3.1
- kbnosurvey kbarchive kbhowto kbcrt KB140584