1

我正在创建一个 DLL 并FastString使用 CreateFastString函数提供类的入口点:

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文件的路径。

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

4

1 回答 1

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.cpp使用MY_DLL_EXPORTS defined构建,以便从生成的 DLLMY_DLL_PUBLIC导出所有标记的符号。"Exported"意味着它们将对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. 最后,我在您发布的代码中没有看到任何包含保护,并且在我的示例中也省略了它们,但请注意您应该肯定拥有它们,所以不要忘记将它们添加到所有标题中,如果它们您的真实代码中缺少。

于 2013-05-13T17:56:00.060 回答