簡體   English   中英

錯誤LNK2019:函數_wmain中引用的未解析的外部符號_CreateFastString

[英]error LNK2019: unresolved external symbol _CreateFastString referenced in function _wmain

我正在創建一個DLL,並使用CreateFastString函數提供了FastString類的入口點:

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

任何人都可以建議出什么問題了。 提前致謝。

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;
}

注意:使用MY_DLL_EXPORTS 定義構建FastString.cpp ,以便從生成的DLL中導出所有標有MY_DLL_PUBLIC的符號。 “已導出” ,表示它們對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 最后,在您發布的代碼中沒有看到任何包含保護 ,並且在示例中也省略了它們,但是請注意,您應該確定要使用它們,因此,如果它們包含所有標頭,也請不要忘記添加它們您的真實代碼中缺少。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM