Avatar of Tamije
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/SingleEndFaultLocation.lib and object Debug/SingleEndFaultLocation.exp
Debug/SingleEndFaultLocation.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
C++

Avatar of undefined
Last Comment
Tamije

8/22/2022 - Mon
jkr

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.
itsmeandnobodyelse

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
ASKER CERTIFIED SOLUTION
itsmeandnobodyelse

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Tamije

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\SingleEndFaultLocation\SingleEndFaultLocation.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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
itsmeandnobodyelse

>>>> 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
itsmeandnobodyelse

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
Tamije

ASKER
Thanks Alex.

Regards
Tamije
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.