I am trying to create a thread safe singleton class(MySingleton). A.h contains declaration and A.cpp has definitions. I need to use this singleton object in B.cpp and C.cpp which are both getting compiled into different DLLs. In B.cpp I want to set the variable ptrval and in C.cpp I am retrieving variable ptrval.
In A.h
class MySingleton {
public:
static MySingleton* getInstance();
void setval(int* uid);
int* getval();
private:
MySingleton() = default;
~MySingleton() = default;
MySingleton(const MySingleton&) = delete;
MySingleton& operator=(const MySingleton&) = delete;
int* ptrval = nullptr;
static MySingleton* instance;
};
#ifdef MySingleton_DLLEXPORT
extern __declspec(dllexport) int* ptrval = nullptr;
extern __declspec(dllexport) MySingleton* getInstance();
extern __declspec(dllexport) void setval(int* uid);
extern __declspec(dllexport) int* getval();
extern __declspec(dllexport) MySingleton* instance = 0;
#else
extern __declspec(dllimport) int* ptrval;
extern __declspec(dllimport) MySingleton* getInstance();
extern __declspec(dllimport) void setval(int* uid);
extern __declspec(dllimport) int* getval();
extern __declspec(dllimport) MySingleton* instance;
#endif
In A.cpp
#pragma push_macro("MySingleton_DLLEXPORT")
#define MySingleton_DLLEXPORT
#include <A.h>
#pragma pop_macro("MySingleton_DLLEXPORT")
MySingleton* MySingleton::instance;
MySingleton* MySingleton::getInstance()
{
if (instance == 0)
instance = new MySingleton();
return instance;
}
void MySingleton::setval(int* uid) {
this->ptrval = uid;
}
int* MySingleton::getval() {
return this->ptrval;
}
In B.cpp
#pragma push_macro("MySingleton_DLLEXPORT")
#include <A.h>
#pragma pop_macro("MySingleton_DLLEXPORT")
int* uid = (getting uid from a function)
(MySingleton::getInstance())->setval(uid);
In C.cpp
#pragma push_macro("MySingleton_DLLEXPORT")
#include <A.h>
#pragma pop_macro("MySingleton_DLLEXPORT")
ptrval = (MySingleton::getInstance())->getval();
I am getting the following linking error: B.obj : error LNK2019: unresolved external symbol "public: static class MySingleton* __cdecl MySingleton::getInstance(void)"
Any help would be appreciated!
Aucun commentaire:
Enregistrer un commentaire