C++ DLL 链接器错误

标签 c++ api class dll linker

我想为设备的 api 编写一个 dll。因为我是 dll 的新手,所以我想在一个简单的文本编辑器上实现它,然后为 api 制作一个。 我已经制作了头文件和 cpp 文件,但是当我运行代码时,我得到错误 lnk2001,然后是 lnk1120,这是 Unresolved external 错误。

我真的不知道我在哪里犯了错误,据我所知我做对了。我想知道你们是否可以帮助我。谢谢。

这是我的头文件

// 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 文件中,我只定义了我在头文件中声明的函数。

这些是我得到的错误

Error 25 error LNK2001: unresolved external symbol "private: static class 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

最佳答案

你的 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);
        .
        .
        .

关于C++ DLL 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8882015/

相关文章:

java - 如何隐藏我的 Dropbox API key

.net - .net youtube库集成

php - WooCommerce 产品 SKU 检查不起作用

c++ - 什么放在cpp和什么到h文件

api - YouTube搜索API给我的视频少于每页请求的视频,但是给了我nextPageToken

c++ - 为什么我的类构造函数没有按预期工作?

javascript - 在 Javascript 中查找类元素名称时遇到问题

c++ - 使用 sort() 对字符数组进行排序

c++ - 为什么我可以在 map 中使用 push_back 但不能在 set 中使用 back_inserter?

c++ - 如何在 Doxygen 中记录一个单词,它是代码中的实体名称,但在当前上下文中不是?