c++ - 错误 LNK2005 : "void __cdecl operator delete(void *)" (? ?3@YAXPAX@Z) 已在 LIBCMTD.lib(delete_scalar.obj) 中定义

标签 c++ mfc static-libraries linker-errors crt

我编写了一个覆盖删除操作符的 c++ 静态库。
在测试项目中使用库时,项目产生如下错误:

错误 LNK2005:“void __cdecl operator delete(void *)” (??3@YAXPAX@Z) 已在 LIBCMTD.lib(delete_scalar.obj) 中定义

搜索了答案后,我相信这是由于 CRT 和 MFC 库的包含顺序造成的,但是在尝试应用解决方案时,所有答案都过时了。我正在使用 Visual Studio 2017。

谁能告诉我如何正确构建我的库?谢谢。

编辑 - 我知道一个定义规则。我试图找出如何防止包含 LIBCMTD.lib 版本。

最佳答案

看看这里:

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

正如您将在文章中看到的,有几个原因。例如:

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.



过去我不得不这样做。

关于c++ - 错误 LNK2005 : "void __cdecl operator delete(void *)" (? ?3@YAXPAX@Z) 已在 LIBCMTD.lib(delete_scalar.obj) 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46638252/

相关文章:

c++ - 检查 std::fstream 是否处于写入或读取模式

c++ - C++的专业品质图表库

c - 如何使用 CodeBlocks 创建 C 库?

c - 如何在 Linux 或 Macos 中为 Windows 编译静态 .lib 库

linker - 链接具有相互依赖关系的静态 fortran 库

c++ - 如果在重载的输出运算符函数中使用 cout 怎么办?

c++ - 在什么情况下,c++ 会将变量初始化为零?

c++ - MFC中如何从CWinAppEx访问CView

c++ - Thrift 或 Protocol buffer 作为跨语言序列化解决方案?

c++ - 如何打印 Linux 下 C++ 程序中调用的函数?