【问题标题】:C++ DLL linker errorC++ DLL 链接器错误
【发布时间】: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 做到了。还是不行

标签: c++ api class dll linker


【解决方案1】:

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

【讨论】:

  • OMG 它的工作原理:D 谢谢。所以每当我定义一个静态成员时,我也应该在相应的 cpp 中定义它。对?再次感谢。
【解决方案2】:

在您的 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 文件中,您可以在#include DLL 标头之前#define EDIT_FUNCS_DLL_IMPLEMENTATION

// EditFuncsDll.cpp

#define EDIT_FUNCS_DLL_IMPLEMENTATION
#include "EditFuncsDll.h"

// ... implementation code

【讨论】:

  • 谢谢,实际上我不确定 dllimport 到底是做什么的。我使用 msdn 作为我的 dll 教程,有时不清楚为什么以及何时应该使用它。我正在寻找一个关于 dll 的好教程,但还没有找到。顺便说一句,代码现在构建成功,但是当我运行它时,当 dll 的函数返回时,程序崩溃了,我不明白为什么???!!!可以来自这些预处理器宏吗?
  • @Erfan:我推荐 CodeProject 的这些文章:Regular DLL Tutor For BeginnersHowTo: Export C++ classes from a DLL。崩溃的原因可能不同...您可能想提供更多详细信息(例如,您是否使用相同的 VC++ 编译器和相同的 CRT 风格构建 EXE 和 DLL?)。
  • 是的,我正在使用 VS2011 构建 DLL 和 .exe。我正在为设备制作 API。非常感谢您提供的链接,我只是找不到任何有趣的教程。它要么太旧,要么只是一个 Hello World DLL。
猜你喜欢
  • 1970-01-01
  • 2015-10-07
  • 2020-09-10
  • 1970-01-01
  • 2012-01-15
  • 2021-07-21
  • 1970-01-01
  • 2016-12-07
  • 2011-10-24
相关资源
最近更新 更多