【发布时间】:2012-01-16 15:16:53
【问题描述】:
我想为设备的 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,class std::allocator >,class std::allocator,class 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
和
Error 26 error LNK1120: 1 unresolved externals C:\Users\Lucy\Desktop\Erfan\Text_Editor_DLL\Debug\Text_Editor_DLL.dll Text_Editor_DLL
【问题讨论】:
-
@Assem 但我只是在 cpp 文件中定义函数,这是标头的 cpp 文件,而不是主 cpp。我不认为我应该在那里定义它。我应该吗?
-
你应该定义在相应cpp中声明的每个静态成员变量。只需将它写在 .cpp 文件中的函数定义之前。顺便说一句,我应该修复我在之前的评论中写的:std::vector EditFuncs::MyEditFuncs::MyTextBox;
-
@Assem here is part of my cpp '// EditFuncsDll.cpp #include "EditFuncsDll.h" #include
#include using namespace std;命名空间 EditFuncs { 静态 std::vector<:string> MyTextBox; void MyEditFuncs::Load(string command) { string filename; // 文件名从命令的第五个字符开始到最后 filename = command.substr(5,command.size()); ifstream inFile; inFile.open(文件名);'所以根据你说的我添加了那个静态变量的定义,但我仍然得到同样的错误。我不知道为什么! -
删除 .cpp 中 std::vector 之前的“静态”一词<:string> MyTextBox;
-
@Assem 做到了。还是不行