Tamije
asked on
Lnk2005 error
Hi Experts
I'm developing a C++ DLL project that was working properly till yesterday. And suddenly today, it is giving the following link error when I tried to build it. The frustrating part is I did not make any changes to the project !!!
-------------------------- --------
Linking...
nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj)
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(dbgdel.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in LIBCMTD.lib(new2.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)
nafxcwd.lib(dllmodul.obj) : warning LNK4006: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj); second definition ignored
nafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj); second definition ignored
nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj); second definition ignored
nafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in LIBCMTD.lib(new2.obj); second definition ignored
nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj); second definition ignored
Creating library Debug/SingleEndFaultLocati on.lib and object Debug/SingleEndFaultLocati on.exp
Debug/SingleEndFaultLocati on.dll : fatal error LNK1169: one or more multiply defined symbols found
-------------------------- --------
I searched other threads for this error. But still not able to figure out how to solve it.
In my project, if I comment out the following line, then the link error disppears.
#include <AfxTempl.h>
But I need this header to use CArray template.
Can somebody please help me in solving this problem.
Thanks
Tamije
I'm developing a C++ DLL project that was working properly till yesterday. And suddenly today, it is giving the following link error when I tried to build it. The frustrating part is I did not make any changes to the project !!!
--------------------------
Linking...
nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj)
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(dbgdel.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in LIBCMTD.lib(new2.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)
nafxcwd.lib(dllmodul.obj) : warning LNK4006: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj); second definition ignored
nafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj); second definition ignored
nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj); second definition ignored
nafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in LIBCMTD.lib(new2.obj); second definition ignored
nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj); second definition ignored
Creating library Debug/SingleEndFaultLocati
Debug/SingleEndFaultLocati
--------------------------
I searched other threads for this error. But still not able to figure out how to solve it.
In my project, if I comment out the following line, then the link error disppears.
#include <AfxTempl.h>
But I need this header to use CArray template.
Can somebody please help me in solving this problem.
Thanks
Tamije
Set your project to "Use Runtime Library: Multithreaded (Debug) DLL" (Project Settings|C++|Code Generation). 'libcmtf.lib' links the C runtime statically, which is not a good idea in general.
Check the following links:
http://support.microsoft.com/default.aspx?scid=kb;en-us;q148652
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B112297
Generally, the error occurs either because of mixing multithreaded and non-multithreaded C runtime libraries (that's why jkr recommended to choose the multithreaded code generation) or because of linking the Microsoft MFC libraries together with non-MFC libraries and have a wrong order of these libraries. Check the links for a resolution if jkr's answer doesn't help.
Regards, Alex
http://support.microsoft.com/default.aspx?scid=kb;en-us;q148652
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B112297
Generally, the error occurs either because of mixing multithreaded and non-multithreaded C runtime libraries (that's why jkr recommended to choose the multithreaded code generation) or because of linking the Microsoft MFC libraries together with non-MFC libraries and have a wrong order of these libraries. Check the links for a resolution if jkr's answer doesn't help.
Regards, Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Alex
As you had suggested I got rid of the <AfxTempl.h> header and used std::vector instead.
The link error dissappeared. However, I'm getting the following warning. Can you please explain what this warning means.
-------------------------- --------
c:\My Projects\Areva\SingleEndFa ultLocatio n\SingleEn dFaultLoca tion.h(48) : warning C4251: 'CSingleEndFaultLocation:: m_VaSignal ' : class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class 'CSingleEndFaultLocation'
with
[
_Ty=double
]
-------------------------- --------
Thanks
Tamije
As you had suggested I got rid of the <AfxTempl.h> header and used std::vector instead.
The link error dissappeared. However, I'm getting the following warning. Can you please explain what this warning means.
--------------------------
c:\My Projects\Areva\SingleEndFa
with
[
_Ty=double
]
--------------------------
Thanks
Tamije
>>>> Can you please explain what this warning means.
Yes, it's a message you can ignore. If using a template class in a DLL class that was exported, the compiler complains cause the template class in STL has no export specifier. However, template classes only can exported for a real type e. g. std::vector<int>. But if you would do so, you most likely would get linker errors because of duplicate object modules. Simply, ignore the warning cause if calling the class function that uses the template, the calling source will use the same STL header files as the DLL and all is fine, i. e. it doesn't need the template's export interface.
Regards, Alex
Yes, it's a message you can ignore. If using a template class in a DLL class that was exported, the compiler complains cause the template class in STL has no export specifier. However, template classes only can exported for a real type e. g. std::vector<int>. But if you would do so, you most likely would get linker errors because of duplicate object modules. Simply, ignore the warning cause if calling the class function that uses the template, the calling source will use the same STL header files as the DLL and all is fine, i. e. it doesn't need the template's export interface.
Regards, Alex
You may get rid of the warning by adding the preprocessor statement
#pragma warning (disable : 4251)
at the top of each cpp.
If using precompiled header files (I would like to recommend against that option) you need to add the pragma statement in the header file that is used for PCH or it might get ignored.
Regards, Alex
#pragma warning (disable : 4251)
at the top of each cpp.
If using precompiled header files (I would like to recommend against that option) you need to add the pragma statement in the header file that is used for PCH or it might get ignored.
Regards, Alex
ASKER
Thanks Alex.
Regards
Tamije
Regards
Tamije