Take you to the visual studio--take you out of the pit Dad's runtime library pit

Source: Internet
Author: User
Tags vc runtime



Previous article take you to the visual studio--take you to understand Microsoft's precompiled header technology, we understand Microsoft's precompiled header technology, precompiled way to make our project compiled faster; This article will continue to introduce another Microsoft technology, the runtime Library runtime.



In C + + development under Windows, it is unavoidable to interact with the underlying library of Windows, but a setting of VS is MT, MTd, MD, and MDD that are often confusing and believe that many people have been trapped by him, especially when you use a lot of third libraries. And easy to appear various link problems. Take a look at the following error message:


LIBCMT.lib (_file.obj): Error LNK2005: ___initstdio already defined in LIBC.lib (_file.obj)
LIBCMT.lib (_file.obj): Error LNK2005: ___endstdio already defined in LIBC.lib (_file.obj)


How many people have been trapped by this thing, please raise your feet! Ha ha......



Since there is so much trouble here, it is necessary for us to have a deep understanding of it and know what it is that we can do without fear!


What is the runtime Library?


The runtime library is the runtime repository, or CRT (C run Time Libraries). is the library file that is required by the program at run time, usually the runtime library is provided in the form of Lib or DLL.



Windows C Runtime Library is a Microsoft implementation of the C standard library functions, so that each program can directly use the C standard library functions, and then appeared in C + +, then the C Runtime Library based on the development of C + + Runtime library, which implements support for the C + + standard library. As a result, the C + + runtime library under Windows now contains both the child C standard library and the C + + standard library. If you installed VS2010, under the installation directory under VC\CRT\SRC (such as my directory is C:\Program Files (x86) \microsoft Visual Studio 10.0\VC\CRT\SRC) has the runtime Library (CRT) source code , there are both C files (such as output.c, stdio.h, etc.), as well as C + + files (such as iostream, String).



Before the C runtime library appeared, many programs were written in C, and these programs all use the standard C library, in the previous way each program will eventually have to copy a standard library implementation into the program, so that at the same moment in memory may have many copies of the standard library code (one copy of the program), As a result, Microsoft has made the standard C library a dynamic link for efficiency reasons, so that only one copy is available in memory when multiple programs use the C standard library.



In fact, the runtime library refers to the dynamic libraries (DLLs) that implement these underlying functions, run-time libraries and normal DLLs, which are loaded only when the program is used and do not reside in memory when no program is in use. That being said, there are many systems that may also be written in C, where the existence of these things makes the C runtime inventory in memory, so the runtime library is almost always needed. Although the runtime library should be a dynamic library, but it is customary for us to compile the same code as the Dynamic Runtime Library is also known as the Runtime Library, so VC + + run-time library has ML, MLd, MT, MTd, MD, MD Six kinds (this will be said later).


The main role of the run-time library
    1. Provides support for C standard libraries (such as memcpy, printf, malloc, etc.), C + + standard library (STL).

    2. The application adds a startup function, the main function of the startup function is to initialize the program to be done, assign the initial value to the global variable, and load the user program's entry function.

      The entry point for a console program that does not adopt a wide character set is mainCRTStartup (void). Let's take this function as an example to analyze what kind of entry program The runtime Library has added to us. This function is defined in CRT0.C, and the following code is compiled and simplified by the author:

void mainCRTStartup (void)
{
Int mainret;
/ * Get complete version information of WIN32 * /
_Osver = GetVersion ();
_Winminor = (_osver >> 8) & 0x00FF;
_Winmajor = _osver & 0x00FF;
_Winver = (_winmajor << 8) + _winminor;
_Osver = (_osver >> 16) & 0x00FFFF;
_Ioinit (); / * initialize lowio * /
/ * Get command line information * /
_Acmdln = (char *) GetCommandLineA ();
/ * Get environmental information * /
_Aenvptr = (char *) __crtGetEnvironmentStringsA ();
Set_setargv (); / * Set command line parameters * /
_Setenvp (); / * Set environment parameters * /
_Cinit (); / * C data initialization: global variable initialization, here! * /
__Initenv = _environ;
Mainret = main (__argc, __argv, _environ); / * call main function * /
Exit (mainret);
} 


From the above code, the runtime has done some initialization work before invoking the main or WinMain function of the user program. After initialization is complete, the main or WinMain function that we have written is then called. Only in this way can our C-language runtime libraries and applications work properly.



In addition to CRT0.C, the C run-time library contains wcrt0.c, WINCRT0.C, and wwincrt0.c three files to provide an initialization function. WCRT0.C is a wide-set version of CRT0.C, WINCRT0.C contains entry functions for Windows applications, and WWINCRT0.C is a wide-set version of WINCRT0.C.


Differences and principles of MT, MTd, MD, MDd, (ML, MLd obsolete)


We can set the type of runtime library in use in the Properties->configuration Properties->c/c++->code generation->runtime Library.



Runtime Library





When I take you to the visual studio--, the difference between a static library (LIB) and a dynamic library (DLL) has been explained in detail in the article that you publish your own project library. We know that the compiled static library has only one ProjectName.lib file, while the compiled dynamic library has two files: Projectname.lib+projectname.dll, an import library, a dynamic library.



VC + + has six types of runtime library:


td>vs2003 later discarded
type abbreviation meaning corresponding library name comment
single-threaded /ml release version of single-threaded static library LIBC.lib
single-threaded debug /mld Debug version of single-threaded static library libcd.lib VS2003 later abandoned
multi-threaded /mt release version of multithreading static State Library libcmt.lib
multi-threaded Debug /mtd D Ebug version of multithreaded static library libcmtd.lib
multi-threaded DLL /md multi-threaded dynamic library for release version Msvcrt.lib+msvcrtxx.dll
multi-threaded DLL debug MDd Debug version of multithreaded Dynamic library MSVCRTD . Lib+msvcrtxxd.dll


(1). Static-linked single-wire libraries
A statically linked single-threaded libraries can only be used for single-threaded applications, and the target code of the C run-time library is eventually compiled into the application's binaries. The/ML compilation option allows you to set up a single line of Visual C + + using static links
Libraries
(2). Statically linked multi-line libraries
The target code for statically linked multiline libraries is also eventually compiled in the application's binaries, but it can be used in multithreaded programs. The/MT compilation option allows you to set up a multithreaded library that uses static linking for Visual C + +.
The executable file generated by this option runs without the need for runtime library DLLs to participate, resulting in a slight performance gain, but the resulting binary code becomes very bloated because it is chained to a large runtime library implementation. When an item is embedded in multiple projects as a static-link library, it may result in multiple memory management of the runtime library, resulting in a fatal "Invalid Address specified to Rtlvalidateheap" issue.
(3). Dynamically linked run-time libraries
The dynamically linked run-time library stores all C library functions in a separate dynamic-link library MSVCRTxx.DLL, MSVCRTxx.DLL handles multithreading issues. Use the/MD compilation option to set up Visual C + + usage dynamics.
Links will follow the traditional VC link DLL to the runtime Library MSVCRxx.DLL Import Library MSVCRT.lib link, at run time requires the corresponding version of the VC Runtime Library Redistributable Package (of course, these run-time library DLLs in the application directory is also possible). Because the/MD and/MDd methods do not link the runtime library to the inside of the executable file, the executable file size is reduced effectively. When multiple projects operate in MD mode, the same heap is used internally, memory management is simplified, and cross-module memory management issues are mitigated.



The/MDd,/MLD, or/MTD options use the Debug Runtime Library (debug version of the runtime Library) to correspond to/MD,/ml, or/MT respectively. The debug version of the runtime Library contains debug information, with some protection mechanisms to help detect errors, enhanced detection of errors, and therefore less operational performance than the release version.



Conclusion:/MD and/MDD will be the trend,/ML and/MLD mode please give up in time,/MT and/MTD should not be used when not necessary.


How to avoid this error
    1. /MD and/MDD will be the trend,/ml and/MLD way to give up,/MT and/MTD in the non-essential when it is best not to adopt. try to use/MD,/MDd this way, unless there are special needs, such as the. exe executable file that you want to compile does not need to rely on the runtime Library's. dll;
    2. in multi-engineering development, all projects use the same runtime library. as Utils solution, there are two project:utils and usingutils,usingutils projects to use the Utils project compiled library. If Utils uses the/MDd method, Usingutils also uses/MDD, otherwise it will report a link error.
      If Utils uses MTD, and Usingutils uses/MDD, a redefined error occurs, such as:

      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)


This is because Utils uses MTD in a way that includes the LIBCMTD.lib library, and Usingutils uses/MDD to include Msvcrtd.lib+msvcrtxxd.dll. LIBCMTD.lib and Msvcrtd.lib are compiled with the same code, one is the static library, the import library of a dynamic library, and the LIBCMTD.lib and msvcrtd.lib are definitely duplicated definitions of the same function.
    1. Use the release library when compiling in release mode, using debug libraries when compiling using debug mode. when compiling release version of Usingutils, to use the release method compiled Utils library, compile debug version of Usingutils, to use the Debug method compiled library.
A historical development perspective on the runtime library


Previous review:
Take you to the visual studio--with your understanding of Microsoft's precompiled header Technology



What to tell NEXT:
Take you to play the visual studio--take you understand multibyte encoding with Unicode code



(This article is not finished, tomorrow continues)



Copyright NOTICE: This article for Bo Master original article, without Bo Master permitted not for any commercial use, reproduced please indicate the source.



Take you to the visual studio--take you out of the pit Dad's runtime library pit


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.