gpt4 book ai didi

c++ - 错误 LNK2019 : unresolved external symbol _CreateFastString referenced in function _wmain

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:49 24 4
gpt4 key购买 nike

我正在创建一个 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;
}

注意:构建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 有一些私有(private)函数或类,这些函数或类对 DLL 使用者可见,那么最好用 MY_DLL_PRIVATE 标记它们.最后,我没有看到任何 include guards 在你发布的代码中,我在我的例子中也省略了它们,但要注意你应该肯定有它们,所以如果你的真实代码中缺少它们,请不要忘记将它们添加到你所有的标题中.

关于c++ - 错误 LNK2019 : unresolved external symbol _CreateFastString referenced in function _wmain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16527534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com