尝试使用DLL项目中的类时出现LNK2019错误

LNK2019 error when trying to use a class from a DLL project

本文关键字:错误 LNK2019 DLL 项目      更新时间:2023-10-16

我正在为我的项目开发一个目录监视器类。它是c++中的。dll类型项目。我使用Visual Studio 2013作为我的IDE。

我遵循了这些基本步骤:

1。创建了一个新的项目类型为dll和c++语言

2。增加了类和dllExport类型声明

3。构建项目

4。创建一个console app

类型的新项目

5。添加对dll项目的引用(两个项目在不同的目录中)

6。指向附加包含文件

中头文件的路径

但是在编写一些使用。编译时,我得到以下错误

Error   1   error LNK2019: unresolved external symbol "public: __thiscall DirectoryWatcher::DirectoryWatcher(void)" (??0DirectoryWatcher@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'watcher''(void)" (??__Ewatcher@@YAXXZ) C:UsersKarthikdocumentsvisual studio 2013ProjectsConsoleApplication2ConsoleApplication2Source.obj   ConsoleApplication2*

但是Dll项目构建成功

一开始,我得到了指向析构函数的错误,但在头文件本身中编写了实现(空的只是大括号{})之后。这个错误被这个指向构造函数

的错误所取代。

这是头文件

#define _SCL_SECURE_NO_WARNINGS
#ifdef DIRECTORYWATCHER_EXPORTS
#define APITYPE __declspec(dllexport)
#else
#define APITYPE __declspec(dllimport)
#endif

#if defined(_WIN32) 
#define PLATFORM_WINDOWS
#elif __APPLE__
#define PLATFORM_MAC
#elif __linux
#define PLATFORM_LINUX
#endif
//-------------------------------------------
// Code Begins Here
//---------------------------------
#ifndef DIRECTORY_WATCHER_H
#define DIRECTORY_WATCHER_H
#define USE_DIRENT

//------------------------
// Includes
//--------------
#include<vector>
#include<iostream>
#include<fstream>
#include<string>
#include<sysstat.h>
#include <tchar.h>
#include<map>
#ifdef PLATFORM_WINDOWS
#include<Windows.h>
#endif
#ifdef USE_BOOST
#include<boost/filesystem.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/lexical_cast.hpp>
#endif
#ifdef USE_DIRENT
#include <dirent.h>
#endif
using namespace std;
//Meta File
template<typename T>
struct Meta
{
    string Name,Path;
    size_t GUID;
     float Size;
    int Type;
    // Can be anything like list of name or list of animation files or list of folder with a folder
     vector<T> MetaInfo;
};

//---------------------------------------------
// TYPE DEFS
//-------------------------------------
APITYPE typedef  hash<string> HashFunction;
APITYPE typedef Meta<string> FOLDER_META;

struct ChildrenTree
{
    vector<Meta<string>> Children;
};
struct DirectoryTree
{
    string ParentPath;
    //map<Children Name,Related Info>
    map<string, FOLDER_META> Child;
};
struct Changed_Data
{
    FOLDER_META Old;
    FOLDER_META New;
};
//------------------------------------
//operators
//------------------------------------

#ifdef USE_DIRENT
class DirectoryWatcher
{
    //-----------------Type Defs------------------------
    //typedef Meta<string> FOLDER_META;
public:
    //Varaibles
    FOLDER_META defaultMeta;
    Meta<DirectoryTree> TreeMeta;
    // Default Constructor
    DirectoryWatcher(void);

    ~DirectoryWatcher(void){}  // Eror at first pointed to the destructor which was solved by defining it here its self 
    // Obtains a list of files and folders in a directory
    APITYPE void GetList(const string&, vector<FOLDER_META>* ReturnValue ,FOLDER_META* ThisDireMeta);
    // Searches and Returns the pathto the file
    APITYPE bool FindFile(const string& Path
        ,const string& FileName // File Name
        , string* Ret //Path Returned 
        );
    //Update and check to see if a file as moved or added or changed 
    // Monitor(vector<FOLDER_META>* ChangedFiles,bool* FilesChanged -> return types); 
    APITYPE void Monitor(vector<Changed_Data>* ChangedFiles,bool* FilesChanged);
    // Creates a GUID for a file 
    APITYPE size_t CreateGUID(const string& fileName);
    //Export metadata
    APITYPE void ExportMeta(const string& Path,FOLDER_META meta);
    // Get the meta data
    APITYPE  void GetFolderMeta(const string& Path,Meta<string> * ReturnValue );
    //InitalizeMethod 
    // False if path invalid
    // true if path correct
    APITYPE bool Init(const string& Path //Path to the Project folder (The folder to watch)
        );
    APITYPE bool Init(const string& Path //Path to the Project folder (The folder to watch)
        ,vector<FOLDER_META> * Returnmetas);
    APITYPE void MakeDir(const string& path);

private:
    string Project_Path;
    DIR* dir = nullptr;
    DIR* MainDir = nullptr;
    struct dirent* ent = nullptr;
    DIR* tempDir = nullptr;
    DirectoryTree Tree;
    HashFunction hashFunc;
    //Dpeth Search 
    DirectoryTree UnVisited;

    //-------------------------------------
    // Windows Specifif cCode
    //---------------------------------
    HANDLE ChangeNotifications[2];
    TCHAR lpDrive[4];
    TCHAR lpFile[_MAX_FNAME];
    TCHAR lpExt[_MAX_EXT];
    DWORD Notifications; 

    // Private methods
    APITYPE long GetFolderSize( const string& Path);
    APITYPE bool CheckPathValid(const string& Path);
    APITYPE void RefreshFiles(vector<Changed_Data>* ChangedFiles,const string& path,bool OnlyThisFolder);
    APITYPE void RefreshTree(vector<Changed_Data>* ChangedFiles, const string& path);
    APITYPE void CreateTree(const string& Path );
    APITYPE void SaveTree();
    APITYPE void LoadTree();
    APITYPE bool AreChildEqual(const FOLDER_META& metaA,const FOLDER_META& metaB );
};
#endif
#endif // !DIRECTORY_WATCHER_H

*我省略了实现部分,因为它非常大,但这里只有构造函数*

DirectoryWatcher.cpp

  DirectoryWatcher::DirectoryWatcher(void)
    {
      Project_Path = "";
      dir = nullptr;
      MainDir = nullptr;
      ent = nullptr;
      tempDir = nullptr;
    }

TestAplication

Source.cpp

#include<DirectoryWatcher.h>

using namespace std;


DirectoryWatcher watcher;
// omitted part of code 
int main(int argv, char* argc) 
{
    string Path;
    cout << "enterPath";
    cin >> Path;
    bool ISCahnged;
    vector<FOLDER_META> metas,Returnvalues;
    vector<Changed_Data> changedDatas;
    watcher.Init(Path, &Returnvalues);
    while (true)
    {
        ISCahnged = false;
        watcher.Monitor(&changedDatas, &ISCahnged);
        if (ISCahnged)
        {
            for each (Changed_Data var in changedDatas)
            {
                OutChangedData(var);
            }
        }
    }
    return 0;
}

为更小的代码省略了几行

有谁能帮我整理一下这个问题吗

Thank you

需要从dll中导出类。这里有一个很好的答案:从DLL中导出c++类