VC++常规错误之17:1>nafxcwd.lib(afxmem.obj) : error LNK2005
(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
清除项目。重新编译。搞定