简体   繁体   English

C ++ DLL链接器错误

[英]C++ DLL linker error

I want to write a dll for an api of a device. 我想为设备的api写一个dll。 since i am new to dlls i wanted to implement it on a simple text editor and then make one for the api. 因为我是dll的新手,所以我想在一个简单的文本编辑器上实现它,然后为api创建一个。 I have made header file and cpp file but when i run the code i get error lnk2001 followed by lnk1120 which is unresolved external error. 我已经制作了头文件和cpp文件,但是当我运行代码时,出现错误lnk2001,接着是lnk1120,这是无法解决的外部错误。

I really have no idea where did i make a mistake, as far as i see i did it the right way. 我真的不知道我在哪里犯了错误,据我所知我以正确的方式做了。 i was wondering if you guys could help me out. 我想知道你们是否可以帮助我。 tnx. tnx。

here is my header file 这是我的头文件

// 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();
};
}

and in my cpp file i just define the functions i declared in header file. 在我的cpp文件中,我只定义了在头文件中声明的功能。

and these are the errors i get 这些是我得到的错误

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 错误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

and

Error 26 error LNK1120: 1 unresolved externals C:\\Users\\Lucy\\Desktop\\Erfan\\Text_Editor_DLL\\Debug\\Text_Editor_DLL.dll Text_Editor_DLL 错误26错误LNK1120:1无法解析的外部C:\\ Users \\ Lucy \\ Desktop \\ Erfan \\ Text_Editor_DLL \\ Debug \\ Text_Editor_DLL.dll Text_Editor_DLL

The head of your cpp should be like this : 您的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);
        .
        .
        .

In your DLL's header file you may want to use a preprocessor macro, that expands to __declspec(dllimport) for DLL clients, and to __declspec(dllexport) for code that is implementing the DLL (ie your DLL .cpp files). 在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
{
...
};

In your DLL's source .cpp file(s), you can #define EDIT_FUNCS_DLL_IMPLEMENTATION before #including your DLL header: 在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM