nafxcwd.lib(afxmem.obj) : error LNK2005

来源:互联网 发布:数据建模的方法 编辑:程序博客网 时间:2024/05/16 19:30

(1)错误案例:在写日志程序中出现,工程是MFC程序(注:win32控制台应用程序,不会出现这种错误,当然是不支持MFC库的那种)

(2)错误原因:如下能看出一点眉目,重定义了.在使用***.obj时,已经在***.lib库中定义了.

摘抄:

上网搜了下,是CRT库与MFC库的冲突,解决方法是:让程序先链接Nafxcwd.lib,然后再链接Libcmtd.lib

you've got to change the order inwhich the libraries are linked. This is a bit tricky, since the library are linked automatically, without you explicitly specifying them.

So, first step, tell the linker to ignore the implicit libraries: Project/Setting/Linker, Input Tab, and put "Nafxcwd.lib Libcmtd.lib" in the "Libraries to Ignore" box.

Next, on the same page, in the Object/library Modules box, put the same to libraries. (in you still get the same error, try reversing them on this line)

可以从错误信息里看到,操作符new,delete,delete[]已经在LIBCMTD.lib中定义了,这跟C编译时使用的默认库与MFC运行时的默认编译库编译时链接顺序有关,我们可以手动的改变两个库的编译顺序就能解决这个定义冲突问题。

(3)错误现场:

1>正在链接...
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" () 已经在 LIBCMTD.lib(new.obj) 中定义
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" () 已经在 LIBCMTD.lib(dbgdel.obj) 中定义
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" () 已经在 libcpmtd.lib(newaop.obj) 中定义
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" () 已经在 LIBCMTD.lib(delete2.obj) 中定义
1>C:\Documents and Settings\Administrator\桌面\多线程写日志怎样写\写日志1\Debug\写日志1.exe : fatal error LNK1169: 找到一个或多个多重定义的符号

(4)错误解决办法:

方法一:

 

中文

项目--属性 ---连接器---输入  

                              附加依赖项    空格Nafxcwd.lib Libcmtd.lib

                              忽略指定库    空格Nafxcwd.lib Libcmtd.lib

清除项目。重新编译。搞定

 

 

方法二

解决方案二:找到并纠正出现问题的模块

要查看当前的库链接顺序,请按照下列步骤操作:

  1. 在“项目”菜单上,单击“设置”。
  2. 在“项目设置”对话框的“以下项目的设置”视图中,单击以选中出现链接错误的项目配置。
  3. 在“链接”选项卡上的“项目选项”框中键入 /verbose:lib
  4. 重新生成项目。在链接过程中,这些库将在输出窗口中列出。

方法3

可能上面方法二的那个也要加上 也可能不加 

 项目--属性 ---连接器---命令行 输入: /FORCE:MULTIPLE

 

原创粉丝点击