0

I have dll project. I added to my solution other quite big project that I want to use as a lib in my dll. But when I started to use new project from my dll project, I had several linking errors, also like in this question error LNK2005: new and delete already defined in LIBCMTD.lib(new.obj)

I've put into "Additional dependency" and "ignore specific library" uafxcwd.lib;Libcmtd.lib, and errors about new, delete etc now is gone, but I still got the error about DllMain.

I've also tried to put

extern "C" { int __afxForceUSRDLL; }

line to the cpp with DllMain and got additional error

Error   LNK2005 __afxForceUSRDLL already defined in dllmain.obj 

I'm completely at a loss

Bruice
  • 543
  • 3
  • 11

2 Answers2

0

According to the Doc:Linker Tools Error LNK2005

If two objects define the same symbol, you get exactly this linker error. I suggest you could try to find out which of both libraries you actually need and then tell the linker not to use the other one.

And I suggest you should check the the linker order.The MFC libs need get linked first, and then the CRT libs could be linked.

For more details I suggest you could refer to the link: error LNK2005: _DllMain@12 already defined in MSVCRT.lib

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20
  • But I already have added to additional dependencies uafxcwd.lib; I've also tried all other proposition from the link you've provided, still got same error and even more, when I tried to add extern "C" { int __afxForceUSRDLL; } to my cpp with dllMain – Bruice Feb 26 '21 at 09:25
  • @Bruice I suggest you try to the code: `#ifdef _X86_ extern "C" { int _afxForceUSRDLL; } #else extern "C" { int __afxForceUSRDLL; } #endif` – Jeaninez - MSFT Mar 02 '21 at 07:24
  • I've tried this as as well, got additional "Error LNK2005 __afxForceUSRDLL already defined in dllmain.obj " error – Bruice Mar 02 '21 at 09:40
  • @Bruice _USRDLL and _AFXDLL conflict, I suggest you try the /MD compiler option:[Build Settings for an MFC DLL](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa235516(v=vs.60)?redirectedfrom=MSDN) – Jeaninez - MSFT Mar 03 '21 at 06:26
  • @Bruice I suggest you should provide us with a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)to reproduce the issue. – Jeaninez - MSFT Mar 05 '21 at 01:42
0

I had the exact same problem with a need to use my own DllMain, but it being already defined in a library that I also needed:

error LNK2005: DllMain already defined in uafxcw.lib(dllmodul.obj)

The fix from @Jeaninez to add

#ifdef _X86_ extern "C" 
  int _afxForceUSRDLL;  
#else 
  extern "C" { int __afxForceUSRDLL; } 
#endif

just before my own DllMain worked perfectly. Thanks.

Doug Gillespie
  • 181
  • 1
  • 3