PDA

Click Here To View Forum --> : link-error LNK2005


08-01-2000, 07:12 AM
Hi,

my MFC-Project uses the following settings:

c++:
/nologo /MTd /W3 /Gm /GX /ZI /Od /I ".\include" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "LOGFILE" /FR"Debug/" /Fo"Debug/" /Fd"Debug/" /FD /GZ /c

linker:
ws2_32.lib /nologo /subsystem:windows /incremental:yes /pdb:"Debug/udp_test.pdb" /debug /machine:I386 /out:"Debug/udp_test.exe" /pdbtype:sept

and throws the following error-message on linking:

nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) bereits in (already on) LIBCMTD.lib(new.obj) definiert (defined)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) bereits in (already on) libcpmtd.lib(delop.obj) definiert (defined)

I use the german-version 6.0 Enterprise with SP 3 on WinNT 4 with SP 5.

Does anybody knows about this prob and how to solve it?

Thanks,

Thomas

jrwcode
08-01-2000, 08:34 AM
Hi
What you might want to try and do is go into the project settings and goto the C/C++ tag and then goto the Code Generation Category. There you will find the Use run-time library. You will want to make sure that all you librarys are either all single-threaded or multi-threaded. Have a play around with these settings. That might do the trick.

Hope that helped.

J

m_srinivas
08-01-2000, 09:50 AM
SYMPTOMS
When the C Run-Time (CRT) library and MFC libraries are linked in the wrong order, LNK2005 errors like the following may occur:

nafxcwd.lib(afxmem.obj) : error LNK2005:
"void * __cdecl operator new(unsigned int)"(??2@YAPAXI@Z) already
defined in LIBCMTD.lib(new.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005:
"void __cdecl operator delete(void *)"(??3@YAXPAX@Z) already defined
in 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 in
MSVCRTD.LIB (dllmain.obj)
mfcs42d.lib(dllmodul.obj): error LNK2005: _DllMain@12 already defined in
msvcrtd.lib(dllmain.obj)



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, requiring MFC to be linked before the CRT libraries.

For additional information on weak externals, please see the following article in the Microsoft Knowledge Base:

Q72651 "Weak External" Records: Description, Use, and Errors



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's causing the problem and correct it.

Solution One - Force Linker to Link Libraries in Correct Order
Open the Project Settings dialog box by clicking Settings on the Build menu.


in the Settings For view, select (highlight) the project configuration that's getting the link errors.


Click the Link tab.


Select INPUT in the Category combo box.


In the Libraries to Ignore edit box, insert the library names (for example, Nafxcwd.lib Libcmtd.lib)

NOTE: The linker command line equivalent in /NOD:<library name>


In the Object/library Modules edit box, insert the library names. You must ensure that these are listed in order and as the first two libraries in the line (for example, Nafxcwd.lib Libcmtd.lib).


Solution Two - Find the Problem Module and Correct It
Perform the following steps to see the current library link order:


Open the Project Settings dialog box by clicking Settings on the Build menu.


In the Settings For view, Select (highlight) the project configuration that's getting the link errors .


Click the Link tab.


Type the following in the Project Options dialog box:
/verbose:lib



Rebuild your project. The libraries will now be listed in the output window during the linking process.





STATUS
This behavior is by design.



MORE INFORMATION
When using MFC libraries, you must make sure they are linked before the CRT library. This can be done by ensuring every file in your project includes ..\Msdev\Mfc\Include\Afx.h first, either directly (#include <Afx.h>) or indirectly (#include <Stdafx.h>). The Afx.h include file forces the correct order of the libraries, by using the #pragma comment (lib,"<libname>") 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 will ensure the correct library search order.

Visual C++ does not contain this header file, but you can easily create this file by performing the following steps:



Open ..\Msdev\Mfc\Include\Afx.h.


Select line 29 (#ifndef _AFX_NOFORCE_LIBS) through line 204 (#endif //!_AFX_NOFORCE_LIBS).


Copy the selection to the Windows Clipboard.


Create a new text file.


Paste the contents of the Clipboard into this new file.


Save the file as ..\Msdev\Mfc\Include\Forcelib.h.



Keywords : kberrmsg kbGenInfo kbVC kbVC400 kbVC410 kbVC500 kbVC600
Version : WINNT:4.0,4.1,5.0,6.0;
Platform : NT WINDOWS
Issue type : kbprb