I am working on a directory watcher class for my project.Its .dll type project in c++.I am using Visual Studio 2013 as my IDE.
I followed these basic steps:
1.Created a new project of type dll and c++ language
2.Added the class and dllExport type declaration
3.Build the project
4.Create a new project of type console app
5.Add refernce to the dll project( the two projects are in different directories )
6.Point to the path of header file in Additional include files
But after writing some code that uses.When compiling i get following error
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:\Users\Karthik\documents\visual studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\Source.obj ConsoleApplication2*
But the Dll project build successfully
At the beginning I got error pointing to the destructor but after writing the implementation (empty just braces { }) in header itself.That error was replaced by this one pointing to the constructor
Hers the header file
#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<sys\stat.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
*I Omitted the implementation part as it was very large but here is only the constructor *
DirectoryWatcher.cpp
DirectoryWatcher::DirectoryWatcher(void)
{
Project_Path = "";
dir = nullptr;
MainDir = nullptr;
ent = nullptr;
tempDir = nullptr;
}
The 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;
}
Omitted few lines for smaller code
Can anyone be kind enough to help sort the problem
Thank you