Link to home
Start Free TrialLog in
Avatar of lzha022
lzha022

asked on

error LNK2005:

hello experts,
I am programming on a vc6 dll project. Today i added this line #include <afxmt.h> to my code and it caused the following errors:
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 libcpmtd.lib(delop.obj)
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 libcpmtd.lib(delop.obj); second definition ignored
I have searched the internet for a whole morning but still could not find a solution. Could anyone please help me how to fix this.
Thanks
Alison
Avatar of cup
cup

Check your code generation options.  They should all say "Debug Multithreaded DLL".  It moans about libcmtd when you compile as "Debug Multithreaded".  What this is basically saying is where the runtime routines come from.

single threaded libc
multithreaded libcm
debug single threaded libcd
debug multithreaded libcmtd
multithreaded dll msvcrt
debug multithreaded dll msvcrtd

The code generation option should be the same on all modules.

Alternatively, put the MFC libs first followed by the non-MFC libs and ignore msvcprt, msvcprtd, msvcrt, msvcrtd but this could lead to other link problems.  Some libraries will pull in msvcrt even though the linker has been told to ignore it.  Never did figure out how to override this switch.
>>>> Today i added this line #include <afxmt.h>

afxmt.h automatically will be included via stdafx.h or one of the other afx*.h headers if all your sources were compiled and linked with the multithreading modell.

There is no chance to include <afxmt.h> in a single-threaded module cause you need different runtime libs using the same function names. So, the linker either has duplicate entries or is missing some entries.

When using MFC it already includes the needed runtime functions of the single-threaded or multi-threaded  model. The above error can occur if compiling non-MFC code together with MFC code. You can try to put the libcmtd.lib to the 'Ignore Libraries' in the Linker Input settings.

Regards, Alex
Avatar of lzha022

ASKER

my dll project does not use MFC (project settings->general->Micrsoft Foundation Class->Not using MFC), and i did not include stdafx.h. - I used to include stdafx.h at the beginning of c++ files but the users of the dll changed something and removed stdafx.h.
Now i need to do multi-threading so i have the problem metioned here.
Any other recommends.
Regards, Alison
Avatar of lzha022

ASKER

To cup,
I changed debug multithreaded to debug multithreaded dll and rebuild the project. However i got the following more errors besides the previous errors:

nafxcwd.lib(timecore.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcwd.lib(apphelp.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcwd.lib(filelist.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcwd.lib(appcore.obj) : error LNK2001: unresolved external symbol ___argv
nafxcwd.lib(appcore.obj) : error LNK2001: unresolved external symbol ___argc
Regards,
Alison
>>>> Any other recommends.

There are serious problems when removing MFC and PCH (precompiled headers) and switching from single-threaded to multi-threaded. That is cause MFC dlls had used their own runtime dlls while a non-MFC project links standard runtime dlls like libcmtd.lib (it is the debug multi-threaded version of libc.lib). Unfortunately, you can't punctually remove MFC and get all dependend settings changed, e. g. in the linker input or C++ code generation, and ... . I highly recommend to create a new project (with the same name) from scratch - after saving all old files to a different folder - and copy only source files and resource files to the new project folder. The folder should have made completely empty before creating the project. Finally, add all source/resource files to the new project (Add Existing Item in the project tree) and it should work.

>>>> nafxcwd.lib(timecore.obj) : error LNK2001: unresolved external symbol __mbctype

That is because some of your sources still use MFC (AFX is the Auxiliary Functional eXtension of MFC) but because of removing the afx headers and other mainframe sources now some external type definitions were declared but not defined.  

Regards, Alex




>>>> on a vc6 dll project.

Sorry, in my last post I assumed you had  a VC7 or VC8 project.

In VC6 you add files to a project by selecting the project or subfolder in the project tree and choose 'Add Files to ...' via context menu.

Regards, Alex


Avatar of lzha022

ASKER

I created a new dll project(win32 Synamic-link library) from scrach but i got the same errors again. I did not use MFC this time right?

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 libcpmtd.lib(delop.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 libcpmtd.lib(delop.obj); second definition ignored

Regards
Alison
Avatar of lzha022

ASKER

The link error occurs only after i added
#include <afxmt.h>    //for thread-safe
static CCriticalSection myCriticalSection;
and use myCriticalSection.
Does this mean afxmt.h is MFC lib? What should i do?
Does this mean afxmt.h is MFC lib?

Yes, AFX means MFC. The library used is nafxcwd.lib.

Why do you need the afxmt.h header?

>>>> nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj)

To solve the problem you could try to put libcmtd.lib to the 'Ignore Libraries' settings at Linker - Input.

Regards, Alex



Avatar of lzha022

ASKER

>>Why do you need the afxmt.h header?
The users of the dll would like me to add CCriticalSection to one of the classes so solve thread safe problem.
>To solve the problem you could try to put libcmtd.lib to the 'Ignore Libraries' settings at Linker - Input.
I did this but it caused more than 1000 other errors.

Regards, Alison
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lzha022

ASKER

Thanks Alex.
Working now.
Regards,
Alison