简体   繁体   中英

C++ DLL linker error

I want to write a dll for an api of a device. since i am new to dlls i wanted to implement it on a simple text editor and then make one for the 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.

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.

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.

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

and

Error 26 error LNK1120: 1 unresolved externals 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 :

    #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).

// 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:

// EditFuncsDll.cpp

#define EDIT_FUNCS_DLL_IMPLEMENTATION
#include "EditFuncsDll.h"

// ... implementation code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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