[Reprint] runtime library (runtime library)

Runtime library (runtime library)

HTTPS: // blog.csdn.net/xitie8523/article/details/82712105 

never learned these things 

or heard in class at the time 

or if the world did not take a great teacher speak only care about making money to start a company.

 

Everything from the default library "LIBCMTD" use conflicts with other libraries, use / NODEFAULTLIB: library,

               error LNK2005: XXX already defined in libcmtd.lib in MSVCRT.lib

And Runtime Library does not match: value "MT_staticRealease" does not match the value "MD_DynamicRelease" start

Excerpt: https://blog.csdn.net/pgmsoul/article/details/4203941/

Project - Properties - Configuration Properties --C / C ++ - code generation: he / MT, / MTd, / Md, / MDd four options, you must make use of all libraries use the same configuration, otherwise there will be a prompt, the function may not be resolved even occur. Sometimes we use the library can not control themselves, then the project can only attribute to set the same options you use the library into a river.

Error 1 error LNK2005: _free already defined in libcmtd.lib (dbgheap.obj) MSVCRT.lib

Error 2 error LNK2005: _malloc already defined in libcmtd.lib (dbgheap.obj) MSVCRT.lib 

.....

If there are a bunch of redefinition errors occur in the same lib, but also with a lib, lib then the two functions should be the same as saying the conflict, can choose a 2, as long as the "ignore specific library" within fill need to ignore the library.

 

Project Properties - Configuration Properties - Linker - Input - ignore specific library: libcmtd.lib

Project Properties - Configuration Properties - General -MFC of use: Use MFC in a shared DLL

 

MSVCRT.lib conflict and libcmt.lib is quite common.

As can be seen from the error message is repeated msvcrt.lib library and libcmt.lib defined __isctype like symbols. Why such a problem will happen? This action from both libraries talking about.

msvcrt.lib is VC Multithreaded DLL version of the C runtime library, and libcmt.lib is Multithreaded runtime library. In the same project, all source files must link the same C runtime library. If a file with the Multithreaded DLL version, and other documents with the Single-Threaded or Multithreaded version of the library, that is to say with the different libraries, it will lead to the emergence of this warning.

 

After warning us understand the meaning of information, we are looking for the cause of the problem. In the project settings, we can see that the current project is a non-Multithreaded DLL version of the run-time library, which shows the project as well as other documents not use this version of the runtime library. Obviously, that is, openssl static library. View openssl in nt.mak under ms, we can find a static library version openssl using the compiler switch / MD compiled, that is the default openssl static library use Multithreaded DLL version of the C runtime library.

 

The reason found. So the solution, it is clear that there are two. Anyway, unified library will run two items during.

 

The simplest way is the dynamic library to library items modified to use Multithreaded DLL version of the C runtime. 
In some cases your project may not be able to change the current run-time library, you can nt.mak openssl in / MD switch revised to / MT openssl static library and then recompile it.

 

Default Library "library" with other libraries using the conflict; use / NODEFAULTLIB: library of solutions LNK4098

You tried with incompatible libraries. 

Note 
runtime library now contains a mixture of different types of instructions can be prevented. If you try to use different types of runtime libraries in the same program or use the debug and non-debug version of the runtime library, you will receive this warning. For example, if you compile a file with the use of a runtime library, and another file to compile using another runtime library (such as single-threaded run-time library run-time libraries for multithreaded) and tried to link them, you will get this warning. All source files should be compiled to use the same runtime library. For more information, see Using the runtime library (/ MD, / MT and / LD) compiler option. 


You may be used in the linker / VERBOSE: LIB linker switch determining library search. If you receive an LNK4098, and want to create, such as the use of single-threaded, non-debug runtime executable libraries, use the / VERBOSE: LIB option determines the linker to search the library. Linker output as a library search should be LIBC.lib, rather than LIBCMT.lib, MSVCRT.lib, LIBCD.lib, LIBCMTD.lib and MSVCRTD.lib. Can be used for each library to ignore / NODEFAULTLIB, tells the linker to ignore errors runtime library. 

The following table shows based on run-time library to use a library should be ignored. 

To use the library, please ignore these libraries in the second row of the first row running 
single-threaded (LIBC.LIB) 
LIBCMT.LIB, msvcrt.lib, LIBCD.lib, libcmtd.lib, MSVCRTD.LIB 

multithreading (libcmt.lib) 
libc.lib, msvcrt.lib, libcd.lib, libcmtd.lib,  msvcrtd.lib

use multithreaded DLL (msvcrt.lib) 
libc.lib, libcmt.lib, libcd.lib, libcmtd.lib,  msvcrtd.lib

debugging single-threaded (LIBCD.lib) 
LIBC.LIB, LIBCMT.LIB, msvcrt.lib, libcmtd.lib, MSVCRTD.LIB 

debugging multi-threaded ( libcmtd.lib) 
LIBC.LIB, LIBCMT.LIB, msvcrt.lib, LIBCD.lib, MSVCRTD.LIB 

use the debug multithreaded DLL (MSVCRTD.LIB) 
LIBC.LIB, LIBCMT.LIB, msvcrt.lib, LIBCD.lib, libcmtd.lib 


For example, if you receive this warning, and want to create a non-debug runtime libraries single-threaded version of the executable file, you can use the following options to the linker together: 

/NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib / NODEFAULTLIB: libcd.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib

 

Excerpt:    https://blog.csdn.net/luoweifu/article/details/49055933

In C ++ development under Windows, the inevitable to interact with the Windows base library, but a setting MT in VS, MTd, MD and MDd people are often puzzled, I believe many people he had pit , especially if you use a lot of third library project when, and easy links to all kinds of problems. Look at the following error message: 
LIBCMT.lib (_file.obj): error LNK2005: already defined in ___initstdio LIBC.LIB (_file.obj) 
LIBCMT.lib (_file.obj): error LNK2005: already defined in ___endstdio LIBC.LIB (_file.obj)

     

1. What is Runtime Library?

Runtime Library runtime library is also referred to as CRT (C Run Time Library). Is a program in library files needed to run normally runtime library is provided in the form of Lib or Dll.

Windows under the C Runtime Library is a function of Microsoft's implementation of the C standard library functions, so that each program can directly use the C standard library; later appeared in C ++, so he developed a C ++ Runtime Library in C Runtime Library basis, the realization of It supports C ++ standard library. So now C / C under Windows ++ runtime library both contain sub-standard C library also contains the C ++ standard library. If you installed the VS2010, crt the VC in the installation directory \ \ src (such as my directory is C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ crt \ src) when runtime (CRT) source code files where both C (e.g. output.c, stdio.h, etc.), there are C ++ files (e.g. iostream, string).

Appear before the C Runtime Library, many programs written in C, and these programs have to use the standard C library, according to the way each program before the final copy must achieve a standard library into the program, so the same time memory there may be many parts of the code standard library (a copy of the program), so Microsoft for efficiency reasons the standard C library is implemented as a dynamically linked, so that multiple programs using the C standard library memory in only one copy a.

Rather runtime library refers to the underlying basis of these dynamic library functions implemented (Dll), runtime libraries and ordinary Dll same, only the procedures used by it will be loaded, no program will not use the time in leave memory. Having said that, how many things but maybe the system is written in C, there are these things that will make the inventory of C run-time memory, so the runtime library is almost always needed. Although the runtime library should be a dynamic library, but the habit we have compiled the same code base dynamic run-time static library, also known as run-time library, runtime libraries VC under ++ has ML, MLd, MT, MTd, MD, MD six sets (this will later speak).

The main effect of 1.1 runtime library

  1. Provides C standard library (such as memcpy, printf, malloc, etc.), support for C ++ Standard Library (STL) is.

  2. Add the application launch function, the main function of the program startup function to be performed initialization, the initial values ​​of global variables, function entry load user program.

Entry points without the use of wide characters for console program mainCRTStartup (void). Let's analyze runtime library what kind of added to the entry procedures for our function as an example. This function is defined in crt0.c, the following code after the consolidation and simplification of the author:

<span style="color:#000000"><code><span style="color:#000088">void</span> mainCRTStartup(<span style="color:#000088">void</span>)
{
 <span style="color:#000088">int</span> mainret;
 <span style="color:#880000">/*获得WIN32完整的版本信息*/</span>
 _osver = GetVersion();
 _winminor = (_osver >> <span style="color:#006666">8</span>) & <span style="color:#006666">0x00FF</span> ;
 _winmajor = _osver & <span style="color:#006666">0x00FF</span> ;
 _winver = (_winmajor << <span style="color:#006666">8</span>) + _winminor;
 _osver = (_osver >> <span style="color:#006666">16</span>) & <span style="color:#006666">0x00FFFF</span> ;
 _ioinit(); <span style="color:#880000">/* initialize lowio */</span>
 <span style="color:#880000">/* 获得命令行信息 */</span>
 _acmdln = (<span style="color:#000088">char</span> *) GetCommandLineA();
 <span style="color:#880000">/* 获得环境信息 */</span>
 _aenvptr = (<span style="color:#000088">char</span> *) __crtGetEnvironmentStringsA();
 _setargv(); <span style="color:#880000">/* 设置命令行参数 */</span>
 _setenvp(); <span style="color:#880000">/* 设置环境参数 */</span>
 _cinit(); <span style="color:#880000">/* C数据初始化:全局变量初始化,就在这里!*/</span>
 __initenv = _environ;
 mainret = main( __argc, __argv, _environ ); <span style="color:#880000">/*调用main函数*/</span>
 <span style="color:#4f4f4f">exit</span>( mainret );
}</code></span>
  1.  
    void mainCRTStartup(void)
  2.  
    {
  3.  
      int mainret;
  4.  
      / * Get the full version information WIN32 * /
  5.  
     _osver = GetVersion();
  6.  
     _winminor = (_osver >> 8) & 0x00FF ;
  7.  
     _winmajor = _osver & 0x00FF ;
  8.  
     _winver = (_winmajor << 8) + _winminor;
  9.  
     _osver = (_osver >> 16) & 0x00FFFF ;
  10.  
     _ioinit(); /* initialize lowio */
  11.  
      / * Get the command-line * /
  12.  
     _acmdln = ( char *) GetCommandLineA();
  13.  
      / * Access to environmental information * /
  14.  
     _aenvptr = ( char *) __crtGetEnvironmentStringsA();
  15.  
     _setargv (); / * set the command line parameter * /
  16.  
     _setenvp (); / * set the environment parameter * /
  17.  
     _cinit (); / * initialize data C: initializing global variable, here! * /
  18.  
     __initenv = _environ;
  19.  
     = main mainret (__argc, __argv, _environ); / * main function call * /
  20.  
      exit( mainret );
  21.  
    }

From the above code can be seen, the runtime before calling the main user program or WinMain function, carried out some initialization. After initialization, then it calls the main or WinMain function we write. Only in this way, our C language run-time libraries and applications to work properly together.

In addition crt0.c, C runtime library further comprises wcrt0.c, wincrt0.c, wwincrt0.c three documents to provide initialization function. wcrt0.c wide characters are crt0.c Edition, entry contains the function wincrt0.c windows applications, and is wincrt0.c wwincrt0.c wide characters Edition.

 

2. MT, MTd, MD, MDd, (ML, MLd obsolete) and the principle of distinction

We can Properties-> Configuration Properties-> C / C ++ -> type runtime library using the Code Generation-> Runtime Library set. 

Runtime Library
Runtime Library

 

In take you publish your project library with you Fun Visual Studio-- article has explained in detail the static library (Lib) differs from dynamic libraries (Dll) of. We know only compile a static library ProjectName.lib file, and compile a dynamic library has two files: ProjectName.lib + ProjectName.dll, is to import a library, a dynamic library.

VC ++ Runtime Library There are six types of:

Types of Abbreviation meaning Corresponding library name Remark
Single-Threaded /ML Release version of the single-threaded static library libc.lib After VS2003 abandoned
Single-Threaded Debug / MLD Debug version of the single-threaded static library libcd.lib After VS2003 abandoned
Multi-threaded /MT Release version of the multi-threaded static library libcmt.lib  
Multi-threaded Debug /MTd Debug version of the multi-threaded static library libcmtd.lib  
Multi-threaded DLL /MD Release version of the multi-threaded DLL msvcrt.lib+msvcrtxx.dll  
Multi-threaded DLL Debug MDd Debug version of the multi-threaded DLL msvcrtd.lib+msvcrtxxd.dll  

You can find installation directory VS of these libraries, such as my VS2010 installed in the C: \ Program Files (x86) \ Microsoft Visual Studio 10.0, you can in C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ lib \ and C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ lib \ amd64 \, respectively, to find the corresponding 32-bit and 64-bit libcmt.lib, libcmtd.lib, msvcrt.lib, msvcrtd.lib library. libc.lib and libcd.lib since VS2005 has been abandoned, so it certainly can not be found.

You will find from the above table, the dynamic multi-threaded runtime library is | msvcrt.lib + msvcrtxx.dll, the reason is because the library is msvcrtxx.dll name of each version of VS used is not the same. And not just include a library, MSVCRT addition to the main library, as well as MSVCPRT, MSVCIRT library. Correspondence between them is as follows:

Import library MSVCRT.LIB MSVCRTD.LIB MSVCPRT.LIB MSVCPRTD.LIB MSVCIRT.LIB MSVCIRTD.LIB
Visual C++ 5.0 Msvcrt.dll MSVCRTD.DLL MSVCP5.DLL MSVCP5D.DLL MSVCIRT.DLL MSVCIRTD.DLL
Visual C++ 6.0 Msvcrt.dll MSVCRTD.DLL MSVCP6.DLL MSVCP6D.DLL MSVCIRT.DLL MSVCIRTD.DLL
Visual C++ .NET 2002 MSVCR70.DLL MSVCR70D.DLL MSVCP70.DLL MSVCP70D.DLL    
Visual C++ .NET 2003 msvcr71.dll MSVCR71D.DLL MSVCP71.DLL MSVCP71D.DLL    
Visual C++ 2005 MSVCR80.DLL MSVCR80D.DLL MSVCP80.DLL MSVCP80D.DLL    
Visual C++ 2008 MSVCR90.DLL MSVCR90.DLL MSVCP90.DLL MSVCP90D.DLL    
Visual C++ 2010 MSVCR100.DLL MSVCR100D.DLL MSVCP100.DLL MSVCP100D.DLL    

参考阅读:https://support.microsoft.com/en-us/kb/154753

在你的VS安装目录下(如C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\redist\x64\Microsoft.VC100.CRT),及系统目录C:\Windows\System32、C:\Windows\SysWOW64下都能找到对应的.dll库。

很多的软件在发布自己的产品时也都会带上这些DLL(防止用户的操作系统没有安装VS,或在系统目录下找不到对应的DLL),如我电脑上的百度影音安装目录下就有MSVCR71.DLL(C:\Program Files (x86)\baidu\BaiduPlayer\4.1.2.286\MSVCR71.DLL),WPS的安装目录下有msvcr100.dll(C:\Program Files (x86)\WPS Office\9.1.0.5132\wtoolex\msvcr100.dll)和msvcp100.dll(C:\Program Files (x86)\WPS Office\9.1.0.5132\wtoolex\msvcp100.dll)

(1). 静态链接的单线程库 
静态链接的单线程库只能用于单线程的应用程序, C 运行时库的目标代码最终被编译在应用程序的二进制文件中。通过 /ML 编译选项可以设置 Visual C++ 使用静态链接的单线 
程库。 
(2). 静态链接的多线程库 
静态链接的多线程库的目标代码也最终被编译在应用程序的二进制文件中,但是它可以在多线程程序中使用。通过 /MT 编译选项可以设置 Visual C++ 使用静态链接的多线程库。 
该选项生成的可执行文件运行时不需要运行时库dll的参加,会获得轻微的性能提升,但最终生成的二进制代码因链入庞大的运行时库实现而变得非常臃肿。当某项目以静态链接库的形式嵌入到多个项目,则可能造成运行时库的内存管理有多份,最终将导致致命的“Invalid Address specified to RtlValidateHeap”问题。 
(3). 动态链接的运行时库 
动态链接的运行时库将所有的 C 库函数保存在一个单独的动态链接库 MSVCRTxx.DLL 中, MSVCRTxx.DLL 处理了多线程问题。使用 /MD 编译选项可以设置 Visual C++ 使用动态。 
链接时将按照传统VC链接dll的方式将运行时库MSVCRxx.DLL的导入库MSVCRT.lib链接,在运行时要求安装了相应版本的VC运行时库可再发行组件包(当然把这些运行时库dll放在应用程序目录下也是可以的)。 因/MD和/MDd方式不会将运行时库链接到可执行文件内部,可有效减少可执行文件尺寸。当多项目以MD方式运作时,其内部会采用同一个堆,内存管理将被简化,跨模块内存管理问题也能得到缓解。

/MDd 、 /MLd 或 /MTd 选项使用 Debug runtime library( 调试版本的运行时刻函数库 ) ,与 /MD 、 /ML 或 /MT 分别对应。 Debug 版本的 Runtime Library 包含了调试信息,并采用了一些保护机制以帮助发现错误,加强了对错误的检测,因此在运行性能方面比不上 Release 版本。

结论:/MD和/MDd将是潮流所趋,/ML和/MLd方式请及时放弃,/MT和/MTd在非必要时最好也不要采用了。

 

3. 如何避免这种错误

3.1. /MD和/MDd将是潮流所趋,/ML和/MLd方式请及时放弃,/MT和/MTd在非必要时最好也不要采用了。尽量使用/MD、/MDd这种方式,除非有某些特殊的需要,如希望编译出来的.exe可执行文件不需要依赖运行时库的.dll; 
2. 在多工程开发时,所有的工程使用同一种运行时库。如Utils的Solution下有两个Project:Utils和UsingUtils,UsingUtils工程要使用Utils工程编译出来的库。如果Utils使用了/MDd的方式,UsingUtils也要使用/MDd的方式,否则会报链接错误。 
如果Utils使用MTd的方式,而UsingUtils使用/MDd的方式,则会出现重定义的错误,如:

<span style="color:#000000"><code>1>LIBCMTD.lib(setlocal.obj) : error LNK2005: __configthreadlocale already defined in MSVCRTD.lib(MSVCR100D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __free_dbg already defined in MSVCRTD.lib(MSVCR100D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR100D.dll)
</code></span>

这是因为Utils使用MTd的方式,包含了libcmtd.

  1.  
    1> LIBCMTD.lib(setlocal.obj) : error LNK2005: __configthreadlocale already defined in MSVCRTD.lib(MSVCR100D.dll)
  2.  
    1> LIBCMTD.lib(dbgheap.obj) : error LNK2005: __free_dbg already defined in MSVCRTD.lib(MSVCR100D.dll)
  3.  
    1> LIBCMTD.lib(dbgheap.obj) : error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR100D.dll)

lib库;而UsingUtils使用/MDd的方式,要包含msvcrtd.lib+msvcrtxxd.dll。libcmtd.lib和msvcrtd.lib是用相同代码编译的,一个是静态库,一个动态库的导入库,同时包含libcmtd.lib和msvcrtd.lib肯定就对相同的函数进行了重复的定义。

  1. 以Release方式进行编译时使用Release的库,使用Debug的方式编译时使用Debug的库。如编译Release版本的UsingUtils时,要使用Release方式编译出来的Utils库,编译Debug版本的UsingUtils时,要使用Debug方式编译出来的库。

 

4. 历史发展的角度讲解运行时库

4.1 从操作系统的角度出发

我们知道操作系统一般是用C写的(因为那时还没有C++,C已经算是最高级的计算机语言了),不管是Linux/Unix还是Windows底层都是大量的C代码。在开发操作系统及相应的应用程序时,很多的程序都会用到相同基础功能的函数库。为了方便开发就把经常用到的底层的基础函数封闭成库(不然你每写一个程序都要把这基础功能实现的源代码拷贝一份到自己的工程,或自己再实现一次),于是就有了C运行时库(C Runtime Library),也就是静态库libc.lib(Release版)、libcd.lib(Debug版)。

因为早期的操作系统和程序都相应简单,用户的需求也不高,那时的操作系统还没有多任务、多线程的概念。所以libc.lib、libcd.lib当然只能支持单线程的程序,而无法支持多线程的程序,因此这个运行时库叫Single-Threaded(/ML)的方式。

后来,随着计算机的普及和发展,计算机要完成的任务越来越多,人们对时间和性能的要求也越来越高,为满足这些需求,操作系统就有了多任务的概念,也有了多线程的技术。而之前的运行时库libc.lib、libcd.lib只能用于单线程,已经无法满足很多程序的需要,于是多线程的运行时库也就应运而生,这就是libcmt.lib、libcmtd.lib,也就是/MT、/MTd的方式。

/MT、/MTd解决了多线程的问题,但随着程序的越来越复杂,一个程序可能会用到多个其他程序的库,多个程序可能会用到相同的库,在内存中会保存多份的相同的静态库。假设A程序使用了C.lib,B程序也使用了C.lib,A、B程序同时运行时,在内存中就会同时存在两份C.lib。 

Static library presentation in memory
静态库在内存中的呈现方式


为了解决这个问题,就产生了动态库的技术。于是就有了动态的运行时库Multi-threaded DLL(/MD)、Multi-threaded DLL Debug(/MDd)。多个程序使用同一个动态库,在内存中只会有一份,效果图如下 

DLL presentation in memory
动态库在内存中的呈现方式

 

4.2 从语言的角度

As we all know, first with the C language, and later developed a C ++ language on the basis of C, and C ++ and C compatible for quite a superset of C's. 
Runtime library beginning, only the C run-time library (C Runtime Library), contains an implementation of the standard C function. Later, with the generated C ++, the C ++ program is running again to use the underlying library added in, there will be a run-time library C / C ++ is (C / C ++ Runtime Library) , then the runtime library includes both achieve the standard C function, but also contains an implementation of the C ++ STL.

Thanks for sharing you the power of God, taken from here only for your own access to learning.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11164895.html