vc2008 项目拆分库 出现 LNK2005 库函数重复定义的问题的解决方法

时间:2024-03-26 21:07:45

把项目拆分成模块,以库的方式进行编译,项目编译link时出现重复定义的问题,错误信息如下:

>uafxcwd.lib(afxinl1.obj) : error LNK2005: "protected: __thiscall CObject::CObject(void)" ([email protected]@[email protected]) already defined in uafxcw.lib(except.obj)
1>uafxcwd.lib(afxinl1.obj) : error LNK2005: "public: virtual __thiscall CObject::~CObject(void)" ([email protected]@[email protected]) already defined in uafxcw.lib(olemisc.obj)
1>uafxcwd.lib(afxinl1.obj) : error LNK2005: "public: virtual void __thiscall CObject::Serialize(class CArchive &)" ([email protected]@@[email protected]@@Z) already defined in cutfiledlg.obj
1>uafxcwd.lib(afxinl1.obj) : error LNK2005: "public: static void * __stdcall CObject::operator new(unsigned int,void *)" ([email protected]@[email protected]) already defined in uafxcw.lib(wincore.obj)
1>uafxcwd.lib(afxinl1.obj) : error LNK2005: "public: virtual __thiscall CException::~CException(void)" ([email protected]@[email protected]) already defined in uafxcw.lib(olemisc.obj)
1>uafxcwd.lib(afxinl1.obj) : error LNK2005: "public: __thiscall CSimpleException::CSimpleException(int)" ([email protected]@[email protected]@Z) already defined in uafxcw.lib(except.obj)
1>uafxcwd.lib(afxinl1.obj) : error LNK2005: "public: virtual __thiscall CSimpleException::~CSimpleException(void)" ([email protected]@[email protected]) already defined in uafxcw.lib(except.obj)

1>uafxcwd.lib(afxinl1.obj) : error LNK2005: "public: __thiscall CMemoryException::CMemoryException(int,unsigned int)" ([email protected]@[email protected]@Z) already defined in uafxcw.lib(except.obj)

出现问题的原因是项目工程和库的工程runtime library 设置不一样导致,如下图:

vc2008 项目拆分库 出现 LNK2005 库函数重复定义的问题的解决方法

把所有工程这个设置成一样,编译link通过,问题解决。具体技术原因参考下面信息:

http://blog.csdn.net/luoweifu/article/details/49055933

另一个问题是,在项目CPP中如果没有加#include "stdafx.h"就会报错的问题,提示如下:

fatal error C1010: unexpected end of file while looking for precompile

去掉预编译头文件,解决的方法如下图示:

vc2008 项目拆分库 出现 LNK2005 库函数重复定义的问题的解决方法