[英]C++ DLL linker error
我想为设备的api写一个dll。 因为我是dll的新手,所以我想在一个简单的文本编辑器上实现它,然后为api创建一个。 我已经制作了头文件和cpp文件,但是当我运行代码时,出现错误lnk2001,接着是lnk1120,这是无法解决的外部错误。
我真的不知道我在哪里犯了错误,据我所知我以正确的方式做了。 我想知道你们是否可以帮助我。 tnx。
这是我的头文件
// EditFuncsDll.h
#include <cstdio>
#include <vector>
#include <string>
namespace EditFuncs
{
class MyEditFuncs
{
private:
static std::vector<std::string> MyTextBox;
public:
static __declspec(dllexport) void Load(std::string command);
static __declspec(dllexport) void Save(std::string command);
static __declspec(dllexport) int Lines();
static __declspec(dllexport) void Add(std::string command);
static __declspec(dllexport) void Remove(std::string command);
static __declspec(dllexport) void Insert(std::string command);
static __declspec(dllexport) int wc(std::string command);
static __declspec(dllexport) void GetInfo();
};
}
在我的cpp文件中,我只定义了在头文件中声明的功能。
这些是我得到的错误
错误25错误LNK2001:无法解析的外部符号“私有:静态类std :: vector,类std :: allocator>,类std :: allocator,类std :: allocator>>> EditFuncs :: MyEditFuncs :: MyTextBox“(?MyTextBox @ MyEditFuncs @ EditFuncs @@ 0V?$ vector @ V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ V?$ allocator @ V?$ basic_string @ DU $ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@@ 2 @@ std @@ A)C:\\ Users \\ Lucy \\ Desktop \\ Erfan \\ Text_Editor_DLL \\ Text_Editor_DLL \\ EditFuncsDll.obj Text_Editor_DLL
和
错误26错误LNK1120:1无法解析的外部C:\\ Users \\ Lucy \\ Desktop \\ Erfan \\ Text_Editor_DLL \\ Debug \\ Text_Editor_DLL.dll Text_Editor_DLL
您的cpp头应该是这样的:
#include "EditFuncsDll.h"
#include <iostream>
#include <fstream>
using namespace std;
namespace EditFuncs
{
std::vector<std::string> EditFuncs::MyEditFuncs::MyTextBox;
void MyEditFuncs::Load(string command)
{
string filename; // The name of the file starts at the fifth character of the command and goes to the end
filename = command.substr(5,command.size());
ifstream inFile;
inFile.open(filename);
.
.
.
在DLL的头文件中,您可能希望使用预处理器宏,该宏针对DLL客户端扩展为__declspec(dllimport)
对于实现DLL的代码(即DLL .cpp文件)则扩展为__declspec(dllexport)
)。
// EditFuncsDll.h
#ifdef EDIT_FUNCS_DLL_IMPLEMENTATION
#define EDIT_FUNCS_DLL __declspec(dllexport) // for DLL implementation
#else
#define EDIT_FUNCS_DLL __declspec(dllimport) // for clients
#endif
class EDIT_FUNCS_DLL MyEditFuncs
{
...
};
在DLL的源.cpp文件中,您可以在#包括DLL标头之前#define EDIT_FUNCS_DLL_IMPLEMENTATION
:
// EditFuncsDll.cpp
#define EDIT_FUNCS_DLL_IMPLEMENTATION
#include "EditFuncsDll.h"
// ... implementation code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.