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

Source: Internet
Author: User

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!

1. 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).

1.1 Main role of the runtime 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 WIN32 Full version information */_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 (); /*  access to environmental information  */_aenvptr =  (char *)  __crtgetenvironmentstringsa (); _SETARGV (); /*  set command line arguments  */_SETENVP (); /*  Set Environment Parameters  */_cinit (); /*  C Data initialization: Global variable initialization, right here! */__initenv = _environ; Mainret = main ( __argc, __argv, _environ )  /* Call the main function */exit ( mainret  );} 123456789101112131415161718192021

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.

http://www.gexing.com/tupian/t/%E9%85%92%E6%B3%89%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%A1%93%E5%8F%B0%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E6%97%A0%E9%94%A1%E5%B8%82%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%95%86%E4%B8%98%E5%B8%82%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E5%A4%A9%E5%BF%83%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%8E%89%E6%BA%AA%E5%B8%82%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700

http://www.gexing.com/tupian/t/%E6%B9%98%E6%BD%AD%E5%B8%82%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700

http://www.gexing.com/tupian/t/%E6%B4%9B%E5%AE%81%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%9B%9B%E4%BC%9A%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E9%98%B3%E8%B0%B7%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E9%82%95%E5%AE%81%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%8C%97%E5%85%B3%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E6%B8%85%E6%B2%B3%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%85%B4%E5%AE%BE%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E5%AE%9C%E6%98%8C%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E9%BC%93%E6%A5%BC%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E5%90%AB%E5%B1%B1%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%AE%89%E5%AE%9A%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E4%BA%A4%E5%9F%8E%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E4%B8%9C%E8%BE%BD%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E9%9D%92%E5%8E%9F%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/


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.