I want to use a static global variable as a mutex. When I try to compile the following code:
//header file
class __declspec(dllexport) StateConservator
{
private:
StateConservator();
StateConservator(const StateConservator&);
protected:
const CString m_oldConf;
CContainer& m_container;
static bool x_mutex;
public:
StateConservator(CContainer& container, const CString& conf)
: m_container(container)
, m_oldConf(!x_mutex? container.GetConf():_T(""))
{
if(!x_mutex)
{
x_mutex= true;
m_container.SetConf(conf);
}
}
~StateConservator()
{
if(x_mutex)
{
x_mutex= false;
m_container.SetConf(m_oldConf);
}
}
};
//cpp file
bool StateConservator::x_mutex= false;
//consumer file
StateConservator cs(*pContainer, pDoc->GetConfiguration());
I get the error:
Consumer.obj : error LNK2001: unresolved external symbol "protected: static bool StateConservator::x_mutex" (?x_mutex@StateConservator@@1_NA)
Please, how can I solve the problem?
UPDATE
I created two minimal programs containing only the essential part to test the problem and they work! This is getting even more strange!
UPDATE 2
Notice the __declspec(dllexport) declaration after the class, that was missing.