问题
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual bool __thiscall CxMemFile::Close(void)" (?Close@CxMemFile@@UAE_NXZ)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual unsigned int __thiscall CxMemFile::Read(void *,unsigned int,unsigned int)" (?Read@CxMemFile@@UAEIPAXII@Z)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual unsigned int __thiscall CxMemFile::Write(void const *,unsigned int,unsigned int)" (?Write@CxMemFile@@UAEIPBXII@Z)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual bool __thiscall CxMemFile::Seek(long,int)" (?Seek@CxMemFile@@UAE_NJH@Z)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual long __thiscall CxMemFile::Tell(void)" (?Tell@CxMemFile@@UAEJXZ)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual long __thiscall CxMemFile::Size(void)" (?Size@CxMemFile@@UAEJXZ)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual bool __thiscall CxMemFile::Flush(void)" (?Flush@CxMemFile@@UAE_NXZ)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual bool __thiscall CxMemFile::Eof(void)" (?Eof@CxMemFile@@UAE_NXZ)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual long __thiscall CxMemFile::Error(void)" (?Error@CxMemFile@@UAEJXZ)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual bool __thiscall CxMemFile::PutC(unsigned char)" (?PutC@CxMemFile@@UAE_NE@Z)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual long __thiscall CxMemFile::GetC(void)" (?GetC@CxMemFile@@UAEJXZ)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual char * __thiscall CxMemFile::GetS(char *,int)" (?GetS@CxMemFile@@UAEPADPADH@Z)
1>StdAfx.obj : error LNK2001: 无法解析的外部符号 "public: virtual long __thiscall CxMemFile::Scanf(char const *,void *)" (?Scanf@CxMemFile@@UAEJPBDPAX@Z)
1>StdAfx.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall CxMemFile::CxMemFile(unsigned char *,unsigned long)" (??0CxMemFile@@QAE@PAEK@Z),该符号在函数 "public: void __thiscall CxMemFile::`default constructor closure'(void)" (??_FCxMemFile@@QAEXXZ) 中被引用
1>StdAfx.obj : error LNK2019: 无法解析的外部符号 "public: virtual __thiscall CxMemFile::~CxMemFile(void)" (??1CxMemFile@@UAE@XZ),该符号在函数 "public: virtual void * __thiscall CxMemFile::`scalar deleting destructor'(unsigned int)" (??_GCxMemFile@@UAEPAXI@Z) 中被引用
1>E:\赛智工作项目\TS8bit\sln2\Debug\GelPrj.exe : fatal error LNK1120: 15 个无法解析的外部命令
1>生成日志保存在“file://e:\赛智工作项目\TS8bit\source\Debug\BuildLog.htm”
1>GelPrj - 16 个错误,5 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
原因
在包含头文件中的函数没有引用时,头文件的class 后有__declspec(dllexport)的导出符号标识,而没有添加实现文件,导致出现无法解析的情况,如下列代码
class DLL_EXP CxMemFile : public CxFile中的DLL_EXP 的定义就是__declspec(dllexport)导出标识符。
#if !defined(__xmemfile_h)
#define __xmemfile_h
#include "xfile.h"
//
#if defined(_AFXDLL)||defined(_USRDLL)
#define DLL_EXP __declspec(dllexport)
#elif defined(_MSC_VER)&&(_MSC_VER<1200)
#define DLL_EXP __declspec(dllimport)
#else
#define DLL_EXP
#endif
class DLL_EXP CxMemFile : public CxFile
{
public:
CxMemFile(BYTE* pBuffer = NULL, DWORD size = 0);
~CxMemFile();
bool Open();
BYTE* GetBuffer(bool bDetachBuffer = true);
virtual bool Close();
virtual size_t Read(void *buffer, size_t size, size_t count);
virtual size_t Write(const void *buffer, size_t size, size_t count);
virtual bool Seek(long offset, int origin);
virtual long Tell();
virtual long Size();
virtual bool Flush();
virtual bool Eof();
virtual long Error();
virtual bool PutC(unsigned char c);
virtual long GetC();
virtual char * GetS(char *string, int n);
virtual long Scanf(const char *format, void* output);
protected:
bool Alloc(DWORD nBytes);
void Free();
BYTE* m_pBuffer;
DWORD m_Size;
bool m_bFreeOnClose;
long m_Position; //current position
long m_Edge; //buffer size
};
#endif
解决方案
方法一、去掉导出符号__declspec(dllexport)
方法二、添加实现文件