2012-04-05 188 views
1

我创建了一个DLL项目并成功构建它。然后我试图在另一个项目TEST中使用该DLL,并且出现以下错误。错误LNK2001:与DLL无法解析的外部符号

Error 1 error LNK2001: unresolved external symbol "public: void __thiscall SnoMessage::setRawMessageName(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > >)" ([email protected]@@[email protected][email protected][email protected][email protected]@@@@@[email protected]@@Z) 

我在链接器属性中添加了必需的lib,并且我还在TEST include目录中添加了头文件。所以这个功能正在被识别,但它一直给出这些错误。该DLL包含以下文件的

SnoMessage.h

#pragma once 
#include "StdAfx.h" 
class SnoMessage 
{ 
public: 
    __declspec(dllexport) SnoMessage(void); 
    __declspec(dllexport) ~SnoMessage(void); 
    __declspec(dllexport) void setRawMessageName(CString messageName); 
    __declspec(dllexport) void setRawMessageType(CString messageType); 
    __declspec(dllexport) void setRawMessageAttributes(std::map<CString,CString> attributes); 
    __declspec(dllexport) CString getRawMessageName(); 
    __declspec(dllexport) CString getRawMessageType(); 
    __declspec(dllexport) std::map<CString,CString> getRawMessageAttributes(); 

private: 
    CString messageName; 
    CString messageType; 
    std::map<CString,CString> attributes; 
}; 

SnoMessage.cpp

#include "stdafx.h" 
#include "SnoMessage.h" 


SnoMessage::SnoMessage(void) 
{ 
} 


SnoMessage::~SnoMessage(void) 
{ 
} 

void SnoMessage::setRawMessageName(CString messageName){ 
    this->messageName = messageName; 
} 

void SnoMessage::setRawMessageType(CString messageType){ 
    this->messageType = messageType; 
} 

void SnoMessage::setRawMessageAttributes(std::map<CString,CString> attributes){ 
    this->attributes = attributes; 
} 

CString SnoMessage::getRawMessageName(){ 
    return messageName; 
} 

CString SnoMessage::getRawMessageType(){ 
    return messageType; 
} 

std::map<CString,CString> SnoMessage::getRawMessageAttributes(){ 
    return attributes; 
} 

而且在测试我做了以下内容:

test.cpp

// test.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include "SnoMessage.h" 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    SnoMessage *msg = new SnoMessage(); 
    msg->setRawMessageName("TEST"); 
    return 0; 
} 

让我知道你是否需要更多信息,谢谢。

+0

小心评论-1? – PTBG 2012-04-05 17:28:43

+0

两个项目都使用ATL吗?两个项目都有相同的运行时库吗?检查常规 - > ATL和使用C/C++ - >代码发生 - >运行时库 – devshorts 2012-04-05 18:09:24

+0

@devshorts是,无论是运行库,并使用ATL都在这两个项目均 – PTBG 2012-04-05 18:19:39

回答

10

在DLL中要使用你的出口DEFS一些头定义这个...

MyExports.h

#ifdef SNOMESSAGE_EXPORTS 
#define SNOMESSAGE_API __declspec(dllexport) 
#else 
#define SNOMESSAGE_API __declspec(dllimport) 
#endif 

现在,在您的DLL您刚刚定义SNOMESSAGE_EXPORTS,那么当你的DLL编译你的类和方法将可见的EXE。但是当你在exe中包含相同的头文件时,宏将导入它们而不是导出。

//In the DLL this is == to export, in the executable this is import. Problem solved. 
class SNOMESSAGE_API SnoMessage 
{ 
public: 
//... 
}; 

您不再需要导出每个成员,只是类。

0

我会将整个班级标记为导出,而不仅仅是其成员函数。另外,根据this conversation的建议,您需要根据是否在DLL中包含头文件或使用该DLL的代码来指定__declspec(dllecport)__declspec(dllimport);并在DLL项目中定义守护宏。

0

当您编译DLL你应该有__declspec(dllexport)的,但是当你编译EXE,你应该有__declspec(dllimport的)。最简单的方法是在“在DLL中”和“在DLL之外”时在#define的某个地方有不同的值。还要导出整个班级而不是单独的方法。

相关问题