How to link with the correct C Run-Time (CRT) library
来源:互联网 发布:阿里云香港被墙 编辑:程序博客网 时间:2024/05/16 08:09
There are six types of reusable libraries: • Static Single Threaded Library (Debug/Release) • Static Multithreaded Library (Debug/Release) • Dynamic Link Library (DLL)(Debug/Release) Note Each library has a debug version and a release version.
The DLL is multithread-safe 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 later.
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 that uses 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.
The DLL is multithread-safe 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 later.
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 that uses 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 _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 when it was built by using this command:
dumpbin /all <object>.obj
Look in the section titled RAW DATA #1. In the right-most column, the default libraries will be listed. Back to the top
MORE INFORMATION
A 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: • 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. Beginning with Visual C++ 4.0, the linker will issue a warning (LNK4098) if a resulting module attempts to combine more than one copy of the CRT library. For more information, search the Help file for LNK4098.
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: • 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. Beginning with Visual C++ 4.0, the linker will issue a warning (LNK4098) if a resulting module attempts to combine more than one copy of the CRT library. For more information, search the Help file for LNK4098.
Back to the top
Sample code
The 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
Back to the top
- How to link with the correct C Run-Time (CRT) library
- How to link with the correct C Run-Time (CRT) library
- CRT (C run-time library)
- CRT (C run-time library)
- CRT (C run-time library)
- How To Use the C Run-Time
- 链接时如何选择C Run-Time(CRT) library
- 链接时如何选择C Run-Time(CRT) library
- 链接时如何选择C Run-Time(CRT) library
- When the C Run-Time (CRT) library and Microsoft Foundation Class (MFC) libraries are linked in the w
- Removing the C run-time library (RTL)
- Compiler options to choose C Run-Time Libraries (CRT)
- C Run-time library
- C run-time library
- C run-time library
- 如何理解C run-time library (C运行时库/CRT)
- C运行时库CRT(C Run-time Library)详解
- C运行时库CRT(C Run-time Library)详解
- 无线局域网的七大安全难题及解决方法
- 打造联系客户的纽带--名片的制作
- IBM 经典销售面试题:营销理念的终极挑战
- 资源
- [历史档案]相思
- How to link with the correct C Run-Time (CRT) library
- 一些内核研究的经典电子书籍
- 算法导论示例-InsertSort
- 自信
- 设计模式资源
- 设计模式应用实例(序言)
- 关注
- 5月30日项目讨论会备忘录
- 离开新太