5

I have written a c++ static library that overwrites the delete operator. When using the library in a test project, the project produces the following error:

error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(delete_scalar.obj)

Having searched for an answer, I believe this is due to the include order of CRT and MFC libraries, but all the answers seam outdated when trying to apply the solution. I am using Visual Studio 2017.

Can anyone tell me how to build my library properly? Thanks.

Edit- I am aware of the One Definition Rule. I am trying to find out how to prevent the LIBCMTD.lib version from being included.

Jester
  • 51
  • 1
  • 5
  • 1
    The answers are still correct. Why do you think that they are outdated. Programmers do all the way the same errors, or getting all the way the same problems. Over years... ;) Its all about libraries and search sequence and a symbol that exists in both libraries... – xMRi Oct 09 '17 at 07:02
  • Possible duplicate of [What exactly is One Definition Rule in C++?](https://stackoverflow.com/questions/4192170/what-exactly-is-one-definition-rule-in-c) – IInspectable Oct 09 '17 at 15:44
  • I am aware of the One Definition Rule. I am trying to find out how to prevent the LIBCMTD.lib version from being included. – Jester Oct 11 '17 at 02:53

1 Answers1

1

Have a look here:

https://learn.microsoft.com/en-gb/cpp/error-messages/tool-errors/linker-tools-error-lnk2005

There are several reasons as you will see in the article. For example:

This error can occur if you link more than one version of the standard library or CRT. For example, if you attempt to link both the retail and debug CRT libraries, or both the static and dynamic versions of a library, or two different versions of a standard library to your executable, this error may be reported many times. To fix this issue, remove all but one copy of each library from the link command. We do not recommend you mix retail and debug libraries, or different versions of a library, in the same executable.

To tell the linker to use libraries other than the defaults, on the command line, specify the libraries to use, and use the /NODEFAULTLIB option to disable the default libraries. In the IDE, add references to your project to specify the libraries to use, and then open the Property Pages dialog for your project, and in the Linker, Input property page, set either Ignore All Default Libraries, or Ignore Specific Default Libraries properties to disable the default libraries.

I have had to do this in the past.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • Thanks Andrew. I'm still having trouble setting this up correctly. I have added "libcmtd.lib" to the Ignore Libraries and then added the library back in Additional Dependencies; which now reads "MyLibrary.lib;libcmtd.lib;%(AdditionalDependencies)". but i still get the same error, any suggestions? – Jester Oct 16 '17 at 04:31
  • @Jester Usually if methods of a library are already included in another. Then you ignore the default (since it is compiled into the other library). Have you just tried ignoring the default library and doing a full rebuild? – Andrew Truckle Oct 16 '17 at 08:17
  • Yes, as mentioned in my previous comment; I added libcmt.lib to the Ignore Libraries. I can see this works as I get a bunch of other linker errors relating to other code use in the library: `error LNK2019: unresolved external symbol ___acrt_iob_func referenced in function _printf` so I added this lib back in **after** linking my library, which results in the original outcome. This method is explained [here](https://support.microsoft.com/en-gb/help/148652/a-lnk2005-error-occurs-when-the-crt-library-and-mfc-libraries-are-link) – Jester Oct 16 '17 at 17:21
  • @Jester Have you tried doing a verbose build to see if it tells you anything else? – Andrew Truckle Oct 16 '17 at 17:38
  • [Here](https://pastebin.com/d7CBPYsr) is the verbose linker output. I can only see the original error message here, but maybe you can see any red flags? – Jester Oct 16 '17 at 18:48
  • @Jester Sorry, I can't be of more help. :( I hope you solve your issue. – Andrew Truckle Oct 16 '17 at 21:13