body { margin:0px; padding:0px; background:#f6f6f6; color:#000000; font-size: small; } #outer-wrapper { font:normal normal 100% 'Trebuchet MS',Trebuchet,Verdana,Sans-Serif; } a { color:#DE7008; } a:hover { color:#9E5205; } a img { border-width: 0; } #content-wrapper { padding-top: 0; padding-right: 1em; padding-bottom: 0; padding-left: 1em; } @media all { div#main { float:right; width:66%; padding-top:30px; padding-right:0; padding-bottom:10px; padding-left:1em; border-left:dotted 1px #e0ad12; word-wrap: break-word; /* fix for long text breaking sidebar float in IE */ overflow: hidden; /* fix for long non-text content breaking IE sidebar float */ } div#sidebar { margin-top:20px; margin-right:0px; margin-bottom:0px; margin-left:0; padding:0px; text-align:left; float: left; width: 31%; word-wrap: break-word; /* fix for long text breaking sidebar float in IE */ overflow: hidden; /* fix for long non-text content breaking IE sidebar float */ } } @media handheld { div#main { float:none; width:90%; } div#sidebar { padding-top:30px; padding-right:7%; padding-bottom:10px; padding-left:3%; } } #header { padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; border-bottom:dotted 1px #e0ad12; background:#F5E39e; } h1 a:link { text-decoration:none; color:#F5DEB3 } h1 a:visited { text-decoration:none; color:#F5DEB3 } h1,h2,h3 { margin: 0; } h1 { padding-top:25px; padding-right:0px; padding-bottom:10px; padding-left:5%; color:#F5DEB3; background:#DE7008; font:normal bold 300% Verdana,Sans-Serif; letter-spacing:-2px; } h3.post-title { color:#9E5205; font:normal bold 160% Verdana,Sans-Serif; letter-spacing:-1px; } h3.post-title a, h3.post-title a:visited { color: #9E5205; } h2.date-header { margin-top:10px; margin-right:0px; margin-bottom:0px; margin-left:0px; color:#777777; font: normal bold 105% 'Trebuchet MS',Trebuchet,Verdana,Sans-serif; } h4 { color:#aa0033; } #sidebar h2 { color:#B8A80D; margin:0px; padding:0px; font:normal bold 150% Verdana,Sans-serif; } #sidebar .widget { margin-top:0px; margin-right:0px; margin-bottom:33px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-size:95%; } #sidebar ul { list-style-type:none; padding-left: 0; margin-top: 0; } #sidebar li { margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; list-style-type:none; font-size:95%; } .description { padding:0px; margin-top:7px; margin-right:12%; margin-bottom:7px; margin-left:5%; color:#9E5205; background:transparent; font:bold 100% Verdana,Sans-Serif; } .post { margin-top:0px; margin-right:0px; margin-bottom:30px; margin-left:0px; } .post strong { color:#000000; font-weight:bold; } pre,code { color:#999999; } strike { color:#999999; } .post-footer { padding:0px; margin:0px; color:#444444; font-size:80%; } .post-footer a { border:none; color:#968a0a; text-decoration:none; } .post-footer a:hover { text-decoration:underline; } #comments { padding:0px; font-size:110%; font-weight:bold; } .comment-author { margin-top: 10px; } .comment-body { font-size:100%; font-weight:normal; color:black; } .comment-footer { padding-bottom:20px; color:#444444; font-size:80%; font-weight:normal; display:inline; margin-right:10px } .deleted-comment { font-style:italic; color:gray; } .comment-link { margin-left:.6em; } .profile-textblock { clear: both; margin-left: 0; } .profile-img { float: left; margin-top: 0; margin-right: 5px; margin-bottom: 5px; margin-left: 0; border: 2px solid #DE7008; } #sidebar a:link { color:#999999; text-decoration:none; } #sidebar a:active { color:#ff0000; text-decoration:none; } #sidebar a:visited { color:sidebarlinkcolor; text-decoration:none; } #sidebar a:hover { color:#B8A80D; text-decoration:none; } .feed-links { clear: both; line-height: 2.5em; } #blog-pager-newer-link { float: left; } #blog-pager-older-link { float: right; } #blog-pager { text-align: center; } .clear { clear: both; } .widget-content { margin-top: 0.5em; } /** Tweaks for layout editor preview */ body#layout #outer-wrapper { margin-top: 0; } body#layout #main, body#layout #sidebar { margin-top: 10px; padding-top: 0; } -->

Thursday, July 17, 2008

A LNK2005 error occurs

A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
View products that this article applies to.
function loadTOCNode(){}
Article ID:148652
Last Review:March 13, 2008
Revision:7.0
This article was previously published under Q148652
On This Page
SYMPTOMS
CAUSE
RESOLUTION
Solution One: Force Linker to Link Libraries in Correct Order
Solution Two: Locate and Correct the Problem Module
STATUS
MORE INFORMATION
Steps to Reproduce the Problem in Visual C++ .NET
SYMPTOMS

When the C Run-Time (CRT) library and Microsoft Foundation Class (MFC) libraries are linked in the wrong order, you may receive one of the following LNK2005 errors:
nafxcwd.lib(afxmem.obj) : error LNK2005:"void * __cdecl operator new(unsigned int)"(??2@YAPAXI@Z) alreadydefined in LIBCMTD.lib(new.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005:"void __cdecl operator delete(void *)"(??3@YAXPAX@Z) already definedin LIBCMTD.lib(dbgnew.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005:"void * __cdecl operator new(unsigned int,int,char const *,int)"(??2@YAPAXIHPBDH@Z) already defined in LIBCMTD.lib(dbgnew.obj)
mfcs40d.lib(dllmodul.obj): error LNK2005: _DllMain@12 already defined inMSVCRTD.LIB (dllmain.obj)
mfcs42d.lib(dllmodul.obj): error LNK2005: _DllMain@12 already defined inmsvcrtd.lib(dllmain.obj)
Back to the top
CAUSE
The CRT libraries use weak external linkage for the new, delete, and DllMain functions. The MFC libraries also contain new, delete, and DllMain functions. These functions require the MFC libraries to be linked before the CRT library is linked.
Back to the top
RESOLUTION
loadTOCNode(1, 'resolution');
There are two ways to resolve this problem. The first solution involves forcing the linker to link the libraries in the correct order. The second solution allows you to find the module that is causing the problem and to correct it. Note The following steps are based on Visual C++ 6.0.
Back to the top
Solution One: Force Linker to Link Libraries in Correct Order
loadTOCNode(2, 'resolution');
1. On the Project menu, click Settings.
2. In the Settings For view of the Project Settings dialog box, click to select the project configuration that is getting the link errors.
3. On the Link tab, click to select Input in the Category combo box.
4. In the Ignore libraries box, insert the library names (for example, Nafxcwd.lib;Libcmtd.lib). Note The linker command-line equivalent in /NOD:.
5. In the Object/library modules box, insert the library names. You must make sure that these are listed in order and as the first two libraries in the line (for example, Nafxcwd.lib Libcmtd.lib).To set this option in Visual C++ .NET, read the "Setting Visual C++ Project Properties" online help topic.
Back to the top
Solution Two: Locate and Correct the Problem Module
To view the current library link order, follow these steps:
1. On the Project menu, click Settings.
2. In the Settings For view of the Project Settings dialog box, click to select the project configuration that is getting the link errors.
3. On the Link tab, type /verbose:lib in the Project Options box.
4. Rebuild your project. The libraries will be listed in the output window during the linking process.
Back to the top
STATUS
This behavior is by design.
Back to the top
MORE INFORMATION
When you use the MFC libraries, you must make sure that they are linked before the CRT library is linked. You can do this by making sure that every file in your project includes Msdev\Mfc\Include\Afx.h first, either directly (#include ) or indirectly (#include ). The Afx.h include file forces the correct order of the libraries, by using the #pragma comment (lib,"") directive. If the source file has a .c extension, or the file has a .cpp extension but does not use MFC, you can create and include a small header file (Forcelib.h) at the top of the module. This new header makes sure that thelibrary search order is correct. Visual C++ does not contain this header file. To create this file, follow these steps:
1. Open Msdev\Mfc\Include\Afx.h.
2. Select the lines between #ifndef _AFX_NOFORCE_LIBS and #endif //!_AFX_NOFORCE_LIBS.
3. Copy the selection to the Windows Clipboard.
4. Create a new text file.
5. Paste the contents of the Clipboard into this new file.
6. Save the file as Msdev\Mfc\Include\Forcelib.h.
Back to the top
Steps to Reproduce the Problem in Visual C++ .NET
1. Start Microsoft Visual Studio .NET.
2. On the File menu, point to New, and then click Project.
3. Click Visual C++ Projects under Project Types, and then click MFC Application under Templates.
4. In the Name text box, type Q148652.
5. In the Location text box, type C:\Test, and then click OK.
6. In the MFC Application Wizard dialog box, click Application Type.
7. Click Dialog based under Application type, and then click Use MFC in a static library under Use of MFC.
8. Click Finish.
9. In Solution Explorer, under Source Files select all the three .cpp files.
10. Right-click the three selected files, and then click Remove.
11. Right-click Source files, point to Add, and then click Add New Item.
12. Click C++ files under Templates. In the Name text box, type Aa. Click Open.
13. Paste the following code in the Aa.cpp file: int test(){new int; return 1;}
14. Right-click Source Files, point to Add, and then click Add Existing Item.
15. Select the following files:
• Q148652.cpp
• Q148652Dlg.cpp
• stdafx.cpp
16. Click Open.
17. The files that you selected in step 15 appear under Source Files.
18. Select all four .cpp files under Source Files.
19. Right-click the four .cpp files that you selected, and then click Properties.
20. Expand Configuration Properties, and then expand C/C++.
21. Click Precompiled Headers.
22. Set the Create/Use Precompiled Header property to Not Using Precompiled Headers. Click OK.
23. On the Build menu, click Rebuild Solution.
Back to the top

APPLIES TO
• Microsoft Visual C++ 2008 Express Edition
• Microsoft Visual C++ 4.0 Standard Edition
• Microsoft Visual C++ 4.1 Subscription
• Microsoft Visual C++ 5.0 Enterprise Edition
• Microsoft Visual C++ 6.0 Enterprise Edition
• Microsoft Visual C++ 5.0 Professional Edition
• Microsoft Visual C++ 6.0 Professional Edition
• Microsoft Visual C++, 32-bit Learning Edition 6.0
• Microsoft Visual C++ .NET 2002 Standard Edition
• Microsoft Visual C++ .NET 2003 Standard Edition

Origin Page : http://support.microsoft.com/kb/148652

No comments: