[英]C++ DLL linker error
我想為設備的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,類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
和
錯誤26錯誤LNK1120:1無法解析的外部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);
.
.
.
在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文件中,您可以在#包括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.