2013-05-13 105 views
1

我创建一个DLL,并提供入口点FastString类使用CreateFastString功能:错误LNK2019:无法解析的外部符号功能_CreateFastString引用_wmain

FastString.h

#define EXPORT __declspec(dllexport) 
#define IMPORT __declspec(dllimport) 

class FastString 
{ 
    const int m_length; 
    char* m_str; 

public: 
    FastString(const char* str); 
    ~FastString(); 
    int Length()const; 
    int Find(const char* str)const; 
}; 

extern "C" FastString* CreateFastString(const char* str); 

FastString.cpp

#include "stdafx.h" 
#include <string> 
#include "FastString.h" 

FastString* CreateFastString(const char* str) 
{ 
    return new FastString(str); 
} 

FastString::FastString(const char* str): m_length(strlen(str)), 
             m_str(new char[m_length+1]) 
{} 

FastString::~FastString() 
{ 
    delete[] m_str; 
} 

int FastString::Length()const 
{ 
    return m_length; 
} 

int FastString::Find(const char* str)const 
{ 
    return 1; 
} 

main.cpp

#include "stdafx.h" 
#include <iostream> 
#include "FastString.h" 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    FastString* str = CreateFastString("Hello Dll"); 
    std::cout<<"The length is "<<str->Length()<<std::endl; 
    return 0; 
} 

在编译过程中,我收到以下错误:

TeatApp.obj : error LNK2019: unresolved external symbol _CreateFastString referenced in function _wmain 
D:\MFC\FastString\Debug\TeatApp.exe : fatal error LNK1120: 1 unresolved externals 

Linker -> Input -> Additional Dependencies我已经提供了.lib文件的路径。

任何人都可以建议发生了什么问题。提前致谢。

+0

您忘记正确使用'EXPORT'和'IMPORT'。 – 2013-05-13 17:16:29

+0

@haroogan即使我声明类EXPORT FastString它仍然没有帮助。 – 2013-05-13 17:41:45

+0

我正在为你准备一个答案,坚持下去。 – 2013-05-13 17:42:23

回答

0

config.h

#define MY_DLL_EXPORT __declspec(dllexport) 
#define MY_DLL_IMPORT __declspec(dllimport) 

// Should be defined when MyDLL is built (more details below). 
#ifdef MY_DLL_EXPORTS 
    #define MY_DLL_PUBLIC MY_DLL_EXPORT 
#else 
    #define MY_DLL_PUBLIC MY_DLL_IMPORT 
#endif 

#define MY_DLL_PRIVATE 

#ifdef __cplusplus 
    #define MY_DLL_FUNCTION extern "C" 
#else 
    #define MY_DLL_FUNCTION extern 
#endif 

FastString.h

#include "config.h" 

class MY_DLL_PUBLIC FastString 
{ 
    const int m_length; 
    char* m_str; 

public: 
    FastString(const char* str); 
    ~FastString(); 
    int Length() const; 
    int Find(const char* str) const; 
}; 

MY_DLL_FUNCTION FastString* MY_DLL_PUBLIC CreateFastString(const char* str); 

FastString.cpp

#include "FastString.h" 

#include "stdafx.h" 

#include <string> 

FastString* CreateFastString(const char* str) 
{ 
    return new FastString(str); 
} 

FastString::FastString(const char* str): m_length(strlen(str)), 
             m_str(new char[m_length+1]) 
{} 

FastString::~FastString() 
{ 
    delete[] m_str; 
} 

int FastString::Length()const 
{ 
    return m_length; 
} 

int FastString::Find(const char* str)const 
{ 
    return 1; 
} 

注:构建FastString.cppMY_DLL_EXPORTS定义为,这样所有用MY_DLL_PUBLIC标记的符号都是从生成的DLL中导出“导出”,这意味着它们将会是可见的用于DLL使用者,例如您的应用程序。最佳做法是在编译期间提供此定义。例如,在MSVC上,可以通过在编译器调用中添加/DMY_DLL_EXPORTS来完成。

main.cpp

#include "stdafx.h" 

#include "FastString.h" 

#include <iostream> 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    FastString* str = CreateFastString("Hello Dll"); 
    std::cout<<"The length is "<<str->Length()<<std::endl; 
    return 0; 
} 

注:main.cpp是你的应用程序,这是基本的DLL的消费,因此,你应该它的编译过程中定义MY_DLL_EXPORTS,使标有MY_DLL_PUBLIC所有符号从所使用的DLL中导入导入

如果你的DLL有一些私人函数或类应是该DLL消费者可见的,那么它是将它们与MY_DLL_PRIVATE标记好的做法。最后,我没有在您的发布代码中看到任何include guards,在我的示例中,我也省略了它们,但请注意,您应该确保它们,所以不要忘记将它们全部添加到标题中,如果他们在你的真实代码中缺少。

+0

它适合你吗? – 2013-05-13 18:23:46

+0

是的,它为我工作 – 2013-05-14 13:22:43

相关问题