[英]error LNK2019: unresolved external symbol _CreateFastString referenced in function _wmain
I am creating a DLL and providing the entry point to the FastString
class using CreateFastString
function: 我正在创建一个DLL,并使用
CreateFastString
函数提供了FastString
类的入口点:
FastString.h
: 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
: 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
: 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;
}
During compilation, I am getting the following errors: 在编译期间,出现以下错误:
TeatApp.obj : error LNK2019: unresolved external symbol _CreateFastString referenced in function _wmain
D:\MFC\FastString\Debug\TeatApp.exe : fatal error LNK1120: 1 unresolved externals
In the Linker -> Input -> Additional Dependencies
I have provided the path for the .lib
file. 在
Linker -> Input -> Additional Dependencies
我提供了.lib
文件的路径。
Can anyone suggest what's going wrong. 任何人都可以建议出什么问题了。 Thanks in advance.
提前致谢。
config.h
: 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
: 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
: 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;
}
NOTE: Build FastString.cpp
with MY_DLL_EXPORTS
defined , so that the all the symbols marked with MY_DLL_PUBLIC
are exported from the resulting DLL. 注意:使用
MY_DLL_EXPORTS
定义构建FastString.cpp
,以便从生成的DLL中导出所有标有MY_DLL_PUBLIC
的符号。 "Exported" , means they will be visible for DLL consumers, such as your application. “已导出” ,表示它们对DLL使用者(例如您的应用程序) 可见 。 The best practice is to supply this definition during compilation.
最佳实践是在编译过程中提供此定义。 For example, on MSVC it can be done by adding
/DMY_DLL_EXPORTS
to the compiler invocation. 例如,在MSVC上,可以通过将
/DMY_DLL_EXPORTS
添加到编译器调用中来完成。
main.cpp
: 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;
}
NOTE: main.cpp
is your application, which is basically the DLL consumer, therefore, you should not define MY_DLL_EXPORTS
during its compilation, so that all the symbols marked with MY_DLL_PUBLIC
are imported from the consumed DLL. 注:
main.cpp
为您的应用程序,这基本上是DLL消费者,所以,你不应该定义MY_DLL_EXPORTS
它的编译过程中,使所有标有符号MY_DLL_PUBLIC
从消费DLL 进口 。
If your DLL has some private functions or classes which should not be visible to the DLL consumers, then it's good practice to mark them with MY_DLL_PRIVATE
. 如果您的DLL具有一些私有函数或类,这些私有函数或类对DLL使用者不可见,那么最好将它们标记为
MY_DLL_PRIVATE
。 Finally, I didn't see any include guards in your posted code, and I omitted them too in my examples, but beware that you should have them for sure, so don't forget to add them too all of your headers, if they are missing in your real code. 最后,在您发布的代码中没有看到任何包含保护 ,并且在示例中也省略了它们,但是请注意,您应该确定要使用它们,因此,如果它们包含所有标头,也请不要忘记添加它们您的真实代码中缺少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.