错误LNK2005:LIBCMTD.lib(new.obj)中已定义new和delete

发布于 2024-07-27 03:25:08 字数 779 浏览 12 评论 0原文

我有一个包含两个项目的 Visual Studio 2005 解决方案。 一个是静态库,另一个是用于测试静态库中的功能的可执行文件。 静态库使用MFC。 当我构建解决方案时,出现以下错误。

uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??    3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmtd.lib(newaop.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)

我不知道如何克服这个问题。 有人可以解释一下为什么会出现此错误吗? 任何概述 .lib 文件链接的解释都将受到高度赞赏。

I have a Visual studio 2005 solution that has two projects. One is a static library and the other is a executable used to test the features in the static library. The static library uses MFC. I got the following errors when I built the solution.

uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??    3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmtd.lib(newaop.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)

I do not know how to overcome this. Can some one please explain why this error is occuring. Any explanation that gives an overview of .lib files linkage will be highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

调妓 2024-08-03 03:25:08

CRT 库对 new、delete 和 DllMain 函数使用弱外部链接。 MFC 库还包含 new、delete 和 DllMain 函数。 这些函数需要在链接 CRT 库之前链接 MFC 库。
http://support.microsoft.com/kb/148652

基于VS2005的解决方案(替换Nafxcwd. lib 和 Uafxcwd.lib for ~VS2013)

转到项目>属性>配置属性>链接器>输入

添加到“附加依赖项” -> Nafxcwd.lib Libcmtd.lib

添加到“忽略特定库”-> Nafxcwd.lib;Libcmtd.lib

库的顺序很重要(Nafxcwd.lib;Libcmtd.lib)。

The CRT libraries use weak external linkage for the new, delete, and DllMain functions. The MFC libraries also contain new, delete, and DllMain functions. These functions require the MFC libraries to be linked before the CRT library is linked.
http://support.microsoft.com/kb/148652

Solution based on VS2005 (Replace Nafxcwd.lib with Uafxcwd.lib for ~VS2013)

go to project>properties>configuration properties>linker>input

add to "Additional dependency" -> Nafxcwd.lib Libcmtd.lib

add to "ignore specific library" -> Nafxcwd.lib;Libcmtd.lib

order of libraries is important( Nafxcwd.lib;Libcmtd.lib).

终止放荡 2024-08-03 03:25:08

要尝试的一件事是确保将:

#include "stdafx.h"

作为 .cpp 文件中的第一行。 我确信这不是所有情况下的答案,但它使相同的错误在我的情况下消失。

One thing to try is to make sure you have:

#include "stdafx.h"

as the first line in your .cpp files. I'm sure that's not the answer in all cases, but it made the identical error go away in my case.

浅黛梨妆こ 2024-08-03 03:25:08

我在Visual Studio 2010的MFC解决方案中遇到了这个问题,同时将在共享DLL中使用MFC更改为在静态库中使用MFC 项目-> 属性-> 配置属性-> 一般。

我通过以下方式解决了问题,请找到项目-> 属性-> 配置属性-> 链接器-> 首先输入

在调试模式下:

  • 其他依赖项中添加uafxcwd.lib;Libcmtd.lib
  • 忽略特定默认库中添加uafxcwd.lib;Libcmtd.lib

在发布模式下:

  • 其他依赖项中添加uafxcw.lib;Libcmt.lib
  • 忽略特定默认库中添加uafxcw.lib;Libcmt.lib

注意:

  1. 不要错过两个 .lib 文件之间的 ;
  2. 调试模式下的文件必须添加后缀-d

I meet this problem in a MFC solution of Visual Studio 2010, while changing Use MFC in a Shared DLL into Use MFC in a Static Library in Project -> Properties -> Configuration Properties -> General.

I solve the problem by the following ways, please locate Project -> Properties -> Configuration Properties -> Linker -> Input at first.

In Debug mode:

  • Add uafxcwd.lib;Libcmtd.lib in Additional Dependencies.
  • Add uafxcwd.lib;Libcmtd.lib in Ignore Specific Default Libraries.

In Release mode:

  • Add uafxcw.lib;Libcmt.lib in Additional Dependencies.
  • Add uafxcw.lib;Libcmt.lib in Ignore Specific Default Libraries.

Notice:

  1. Don't miss the ; between the two .lib files.
  2. A suffix -d must be added in the files in Debug mode.
油饼 2024-08-03 03:25:08

确保在 "stdafx.h" 中包含 #include,然后再添加其他包含内容,例如 #include

be sure that you have #include <afx.h> in "stdafx.h" BEFORE other includes like #include <string>

机场等船 2024-08-03 03:25:08

在配置链接器输入中

  • 在附加依赖项中放置 uafxcw.lib;LIBCMT.lib
  • 在忽略特定位置中放置 uafxcw.lib;LIBCMT.lib

in config linker input

  • In additional dependicies put uafxcw.lib;LIBCMT.lib
  • In Ignore specific put put uafxcw.lib;LIBCMT.lib
无声情话 2024-08-03 03:25:08

确保您链接的 C++ 运行时库与静态库和可执行文件上的相同。 检查您的项目属性 C/C++ -> 代码生成 -> 运行时库设置。

Make sure the C++ runtime library that you are linking with is the same on your static library as well as your executable. Check your project properties C/C++->Code generation->runtime library settings.

梦里寻她 2024-08-03 03:25:08

错字。 一种愚蠢的方法是不包含标头,而是包含 cpp。
例如

#include <myclass.cpp> //should be #include <myClass.h>

Typo. One stupid way you got that is instead of include the header, you inlucde the cpp.
e.g.

#include <myclass.cpp> //should be #include <myClass.h>
云雾 2024-08-03 03:25:08

首先,libcmtd.lib 用于调试版本,libcmt.lib 用于生产版本。 仔细检查您是否没有同时包含两者。 要检查的地方之一是配置属性/链接器项目属性的“命令行”部分。

如果您转到项目的属性,并打开配置属性/链接器/输入部分,您可以“Ingore Specific Library”...尝试在该字段中列出 libcmtd.lib。

First, libcmtd.lib is for a debug version and libcmt.lib is for production. Double-check that you're not including both. One place to check is the "Command Line" section of the Configuration Properties/Linker project properties.

If you go to the properties for the project, and open up the Configuration Properties/Linker/Input section, you can "Ingore Specific Library"...try listing libcmtd.lib in that field.

自由如风 2024-08-03 03:25:08

对我来说,我有一个用 _CRTDBG_MAP_ALLOC 编译的静态库,并且应用程序不是用 _CRTDBG_MAP_ALLOC 编译的,我当时收到的是 LNK2005。 我已将应用程序更改为使用 _CRTDBG_MAP_ALLOC 进行编译,并且 LNK2005 消失了。

For me, I have a static library compiled with _CRTDBG_MAP_ALLOC, and the application not compiled with _CRTDBG_MAP_ALLOC, I was receiving then LNK2005. I've changed the application to compile with _CRTDBG_MAP_ALLOC, and the LNK2005 disappear.

花间憩 2024-08-03 03:25:08

解决了这个问题

uafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned __int64)"
  • 在附加依赖项中放入 uafxcw.lib
  • 在“忽略特定”中放置uafxcw.lib

Got rid of the problem

uafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned __int64)"
  • In additional dependicies put uafxcw.lib.
  • In Ignore specific put put uafxcw.lib.
拿命拼未来 2024-08-03 03:25:08

我用 VS2017 创建了两个新项目,一个可以运行,另一个不能运行,所以我比较了有什么区别。 该工作是使用
File > 创建的 新项目> 视觉C++> MFC/ATL> MFC 应用程序
无法正常工作的应用程序是使用
File > 创建的 新项目> 视觉C++> Windows 桌面 > Windows 桌面向导
然后添加 MFC。 在这两种情况下,我都使用 MFC 作为静态库。 我已经想出了两个解决办法。 但在此之前我们必须添加导入,因为第二个项目没有导入!

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes

现在这两个修复中的任何一个对我都有效:

  1. Project > 属性> 配置属性> 一般> 使用MFC将其设置为在共享DLL中使用,这也应该自动设置C/C++> 代码生成> 运行时库多线程调试dll /MDd确保它确实做到了这一点。
    现在尝试编译,对我来说它有效。
  2. 我注意到工作项目在 stdafx.h 中有一些导入,我将它们复制到另一个项目中的 pch.h 中,它起作用了。(保持属性不变,因此使用静态库)。 复制的代码是这样的:
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit

// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions


#include <afxdisp.h>        // MFC Automation classes

更改链接器设置的其他解决方案我尝试了它们,但它们不起作用。
如果有人知道我的解决方案为何有效,我将不胜感激,这很奇怪,为什么在 pch.h 中包含这些标头可以解决链接器问题,而在其他地方包含相同的标头会触发该错误?

I had created two fresh projects with VS2017, one was working the other not, so I compared what was the difference. The one working was created with
File > New Project > Visual C++ > MFC/ATL > MFC Application
the one not working was created with
File > New Project > Visual C++ > Windows Desktop > Windows Desktop Wizard
then adding MFC. In both cases I was using MFC as static lib. I had figured out two fixes. But before that we have to add imports because the second project had NONE!

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes

Now either of the two fixes worked for me:

  1. Project > Properties > Configuration Properties > General > Use of MFC set it to use in a Shared DLL, this should also automatically set C/C++ > Code Generation > Runtime Library to Multi-threaded debug dll /MDd make sure it indeed did that.
    Try compile now, for me it worked.
  2. I noticed the working project had some imports in stdafx.h, I copied them into pch.h in the other project, it worked.(Keeping the properties unchanged, so static lib was used). The code copied was this:
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit

// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions


#include <afxdisp.h>        // MFC Automation classes

The other solutions changing Linker settings I tried them but they did not work.
I would appreciate if somebody knows why my solution works, it is weird, why including those headers in pch.h solves a linker issue whereas including those same headers anywhere else triggers that error??

落日海湾 2024-08-03 03:25:08

检查两个项目的清单文件,确保它们链接相同版本的标准库。 最有可能不是,检查属性->代码生成->标准库链接。

Check the manifest file of both projects, make sure that they are linking the same version of the standard library. Most likely they are not, check the properties->code generation->standard library linking.

浅听莫相离 2024-08-03 03:25:08

我也有类似的问题。 唐尼给出的链接解释了原因。 解决方案是查看错误消息,然后删除涉及的那些库,并按照先 MFC 库、然后 CRT 库的顺序添加这些库。

vs2008中的实现方式阿里给出了。

I also had a similar problem. The link given by Donnie explains the the reason. The solution was to look at the error messages and then removing those libs involved and adding those libs in the order of MFC libs first and then CRT libs.

The way to do that in vs2008 is given by ali.

离旧人 2024-08-03 03:25:08

我还要补充一点,如果您替换了 new/delete 运算符(如果是,请同时替换数组和标量),您可能需要将它们标记为 __forceinline ,以便 obj 不会与 lib 发生冲突。

例如,我这样做是为了强制对齐分配,并且在我这样做之前遇到了同样的问题:

__forceinline void * operator new(size_t size)
{
    return _aligned_malloc(size, 16);
}
__forceinline void operator delete(void* ptr)
{
    _aligned_free(ptr);
}
__forceinline void * operator new [](size_t size)
{
    return _aligned_malloc(size, 16);
}
__forceinline void operator delete [](void* ptr)
{
    _aligned_free(ptr);
}

I will also add that if you have replaced the new/delete operators (and if so, please do the array and the scalar both), you may need to tag them as __forceinline so that the obj doesn't collide with the lib.

For example, I did these to force aligned allocations and had the same trouble until I did that:

__forceinline void * operator new(size_t size)
{
    return _aligned_malloc(size, 16);
}
__forceinline void operator delete(void* ptr)
{
    _aligned_free(ptr);
}
__forceinline void * operator new [](size_t size)
{
    return _aligned_malloc(size, 16);
}
__forceinline void operator delete [](void* ptr)
{
    _aligned_free(ptr);
}
怀念你的温柔 2024-08-03 03:25:08

声明并定义变量的头文件。 可能的解决方案包括:
在.h中声明变量:extern BOOL MyBool; 然后在 .c 或 .cpp 文件中对其进行赋值:BOOL MyBool = FALSE;。
将变量声明为静态。
声明变量 selectany。

https://msdn.microsoft.com/en-us/library/72zdcz6f。 ASPX

A header file declared and defined a variable. Possible solutions include:
Declare the variable in .h: extern BOOL MyBool; and then assign to it in a .c or .cpp file: BOOL MyBool = FALSE;.
Declare the variable static.
Declare the variable selectany.

https://msdn.microsoft.com/en-us/library/72zdcz6f.aspx

朮生 2024-08-03 03:25:08

对我来说,问题是通过改变解决的

项目-> 属性-> 配置属性-> 一般:使用
MFC = 在共享 DLL 中使用 MFC

设置为“使用标准 Windows 库”之前,

我还必须在下设置 /MD 选项

项目-> 属性-> C/C++-> 代码生成:运行时库=
多线程DLL(/MD)

For me the problem was solved by changing

Project -> Properties -> Configuration Properties -> General: Use of
MFC = Use MFC in a Shared DLL

Before it was set to "Use Standard Windows Libraries"

Additionally I had to set the /MD option under

Project -> Properties -> C/C++ -> Code Generation : Runtime Library =
Multi-threaded DLL (/MD)

北方的巷 2024-08-03 03:25:08

我在搜索此答案时遇到的另一个可能原因是:

我不小心在从应用程序(使用预编译的headers)到共享静态库(不使用预编译头)。

Another possible cause that I ran across while searching for this answer:

I accidentally left an #include "StdAfx.h" line at the top of a .cpp file that I moved from the application (which uses precompiled headers) into a shared static library (which doesn't use precompiled headers).

下雨或天晴 2024-08-03 03:25:08

将 Cipher Saw 的解决方案应用于 vs2015 后出现错误

1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "void __stdcall DDX_Control(class CDataExchange *,int,class CWnd &)" (?DDX_Control@@YGXPAVCDataExchange@@HAAVCWnd@@@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "public: int __thiscall CWnd::ExecuteDlgInit(void *)" (?ExecuteDlgInit@CWnd@@QAEHPAX@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "public: void __thiscall CMFCDynamicLayout::GetHostWndRect(class CRect &)const " (?GetHostWndRect@CMFCDynamicLayout@@QBEXAAVCRect@@@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "void __cdecl AfxRegisterMFCCtrlClasses(void)" (?AfxRegisterMFCCtrlClasses@@YAXXZ) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "protected: void __thiscall CMFCControlContainer::PreUnsubclassControl(class CWnd *)" (?PreUnsubclassControl@CMFCControlContainer@@IAEXPAVCWnd@@@Z) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "public: int __thiscall CMFCControlContainer::SubclassDlgControls(void)" (?SubclassDlgControls@CMFCControlContainer@@QAEHXZ) already defined in uafxcwd.lib(afxctrlcontainer2.obj)

可以通过将库列表从 uafxcw.lib;Libcmt.lib 更改为 afxnmcdd.lib;uafxcwd.lib;Libcmtd.lib 来修复这些 错误code>(调试 unicode 构建)

Got errors after applying Cipher Saw's solution to vs2015

1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "void __stdcall DDX_Control(class CDataExchange *,int,class CWnd &)" (?DDX_Control@@YGXPAVCDataExchange@@HAAVCWnd@@@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "public: int __thiscall CWnd::ExecuteDlgInit(void *)" (?ExecuteDlgInit@CWnd@@QAEHPAX@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "public: void __thiscall CMFCDynamicLayout::GetHostWndRect(class CRect &)const " (?GetHostWndRect@CMFCDynamicLayout@@QBEXAAVCRect@@@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "void __cdecl AfxRegisterMFCCtrlClasses(void)" (?AfxRegisterMFCCtrlClasses@@YAXXZ) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "protected: void __thiscall CMFCControlContainer::PreUnsubclassControl(class CWnd *)" (?PreUnsubclassControl@CMFCControlContainer@@IAEXPAVCWnd@@@Z) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "public: int __thiscall CMFCControlContainer::SubclassDlgControls(void)" (?SubclassDlgControls@CMFCControlContainer@@QAEHXZ) already defined in uafxcwd.lib(afxctrlcontainer2.obj)

Was able to fix them by changing libs list from uafxcw.lib;Libcmt.lib to afxnmcdd.lib;uafxcwd.lib;Libcmtd.lib (debug unicode build)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文