C Runtime Library and a deep understanding of the meaning of the compilation options-05 (end)

Source: Internet
Author: User
The following content is from the network and the author is not clear. Transferred from:Http://advancedcj.wordpress.com/2010/06/18/c-runtime-library-5/ 

(Link)

The above describes an example of a successful link even though different versions of the default standard library are specified for each target module. Next, you will see a miserable failure caused by this rigor. Modify mylib. C as follows: # Include
Void Foo ()
{
// Just a test, don't care Memory Leak
_ Malloc_dbg ( 1 , _ Normal_block, _ file __, _ line __);
} Among them, _ malloc_dbg is not an ansi c standard library function. It is a debugging version of malloc provided by the VC standard library. It works with related functions to help developers catch various memory errors. To use it, you must define the _ debug macro. Otherwise, the pre-processor will convert it to malloc automatically. Continue to use Cl / C / MLD mylib. c
Lib / Out: My. Lib mylib. OBJ Compile and package. When you use Link main. OBJ my. Lib What do we see during the link? My God, a bunch of lnk2005 plus an lnk1169 expensive for "Fatal error", of course, the lnk4098 is also indispensable. Is the linker crazy? No, you have wronged the poor linker. I promise it will keep working with my due diligence. E, U, and D are empty at the beginning, and the linker scans main. OBJ, add it to e, add Foo to U, add main to D, and add libc. add lib to the end of the current input file list. Then, scan my. Lib, foo from u to D, add _ malloc_dbg to U, and add libcd. lib to the end of the current input file list. Then scan libc. lib. no target module in lib defines _ malloc_dbg (It only exists in the standard library of the debugging version). Therefore, no module is added to E because of _ malloc_dbg, but each Program Required initialization modules (such as crt0.obj) and their referenced modules (such as malloc. OBJ, free. will be automatically added to E, and U and D will be updated to reflect this change. When the linker completes libc. Lib, only the _ malloc_dbg symbol is left in the U. Finally, handle libcd. lib, dbgheap is found. OBJ defines _ malloc_dbg, so dbgheap. OBJ is added to E, and the unresolved symbols in it are added to u. All other symbols defined by OBJ are also added to D. Then, the disaster will come. Previously, symbols such as malloc were already in D (with libc. malloc in Lib. OBJ is added to E), while dbgheap. OBJ defines many cognominal symbols, including malloc, which triggers a redefinition conflict and the linker has to interrupt the work and report errors. Now we should know that the linker has no responsibility at all, and the responsibility lies with ourselves. We carelessly linked the target file (main. OBJ) with the Library (My. Lib) that is inconsistent with the default standard library version, resulting in a catastrophic event. The solution is simple. Either use the/MLD option to re-compile main. C, or use the/ml option to re-compile mylib. C. In the above example, we have Source code (Mylib. C), so you can recompile these sources with different options Code And package again. However, if a third-party library is used and the source code is not provided, we only need to change the compilation options of our program to adapt to these libraries. But how do I know the default library specified by the target module in the library? In fact, a small tool of vcpus can be used to complete the task. This is dumpbin.exe. Run the following command Dumpbin / Directives my. Lib Find the "linker ctictives" Bootstrap information in the output. You will surely find that each such information contains several strings similar to "-defaultlib: XXXX, XXXX represents the default database name specified by the target module. Knowing the default standard library specified by a third-party library and compiling our application with appropriate options can avoid lnk2005 and lnk1169 Link errors. If you like IDE, you can go to "Project Properties"-> "C/C ++"-> "code generation) "->" Run-Time library "sets the default standard library version of the application, which is the same as the command line option.
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.