How to link with the correct C Run-Time (CRT) library

原创 2006年06月02日 21:59:00
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.
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 _DLL
You 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.


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.

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.

// MyReusableStaticMultithreadReleaseLibrary.h
#if !defined(_MT) || defined(_DLL) || defined(_DEBUG)
    #error The /MT compiler switch is required.

// MyReusableDynamicLinkReleaseLibrary.h
#if !defined(_MT) || !defined(_DLL) || defined(_DEBUG)
    #error The /MD compiler switch is required.

// MyReusableStaticSingleThreadDebugLibrary.h
#if defined(_MT) || !defined(_DEBUG)
    #error The /MLd compiler switch is required.

// MyReusableStaticMultithreadDebugLibrary.h
#if !defined(_MT) || defined(_DLL) || !defined(_DEBUG)
    #error The /MTd compiler switch is required.

// MyReusableDynamicLinkDebugLibrary.h
#if !defined(_MT) || !defined(_DLL) || !defined(_DEBUG)
    #error The /MDd compiler switch is required.

How to link with the correct C Run-Time (CRT) library

SUMMARY There are six types of reusable libraries: Static Single Threaded Library (Debug/Relea...
  • zdcsky123
  • zdcsky123
  • 2016年05月18日 16:36
  • 246

CRT (C run-time library)

一 产生 运行时库是程序在运行时所需要的库文件,通常以LIB或DLL形式提供。 C运行时库就是C run-time library,诞生于20世纪70年代,是C而非C++语言世界的概念,C程序运行...
  • maomaokuaile
  • maomaokuaile
  • 2009年12月24日 16:29
  • 1145

文件操作系列之一——(C Run-Time Library)

现在要做一个东西出来,因为涉及的知识面比较广,所以需要系统的把知识整理一下,刚好用到文件操作这一块,就整理了一下相关的要点。 操作文件的方式很多,这里重点介绍几个,本次介绍C Run-Ti...
  • blpluto
  • blpluto
  • 2010年07月13日 07:12
  • 1584

C运行时库CRT(C Run-time Library)详解

一、什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语...
  • peter_teng
  • peter_teng
  • 2013年09月24日 10:26
  • 2213

how to compile and link static/dynamic library in linux

 for static library:1) use gcc to compile the target files, for example:     gcc –c Strlen.c Strnlen...
  • hanqcheng
  • hanqcheng
  • 2009年12月06日 11:26
  • 1358

一天一个C Run-Time Library 函数(绪)

继续为windows/linux通用服务器框架做研究工作,从C语言运行时库开始。 最好的可移植编程方式是什么?除了java。。。还有C。。。。,标准C是可以在几乎任何有C语言编译器的机器上运行的,这是...
  • vagrxie
  • vagrxie
  • 2008年10月29日 20:06
  • 2082

C Run-time library

以下摘自《edyang 语录》:记住一个人,最重要的是记住他/她/它是男人/女人/东东,有没有贬过,有没有吻过,有没有咬过,名字记不住反而关紧要。C Run-time Library    C ...
  • chjp2046
  • chjp2046
  • 2010年06月10日 21:55
  • 611

Use run-time library 设置问题

使用库时常出现的错误, 提示: 基本是库和程序选择的run-time ibrary不同引起的.LIBCMT.lib(crt0dat.obj) : error LNK2005: _exit alread...
  • wangandy7811
  • wangandy7811
  • 2010年05月21日 15:23
  • 3285

c runtime(CRT)

当C Runtime函数库于20世纪70年代产生出来时,PC的内存容量还很小,多任务是个新奇观念,更别提什么多线程了。因此以当时产品为基础所演化的C Runtime函数库在多线程(multithrea...
  • wzsbll
  • wzsbll
  • 2011年04月21日 16:00
  • 7094

一天一个C Run-Time Library 函数(3) abort

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE ...
  • vagrxie
  • vagrxie
  • 2008年10月31日 23:27
  • 4417
您举报文章:How to link with the correct C Run-Time (CRT) library