C++ 静态数据字段导致的使用者文件中的链接错误
C++ Error linking in consumer file caused by static data field
我想使用静态全局变量作为互斥锁。当我尝试编译以下代码时:
//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());
我收到错误:
Consumer.obj : error LNK2001: unresolved external symbol "protected: static bool StateConservator::x_mutex" (?x_mutex@StateConservator@@1_NA)
请问,我该如何解决问题?
更新
我创建了两个最小的程序,只包含测试问题的基本部分,它们可以工作!这越来越奇怪了!
更新 2
请注意类后面缺少的 __declspec(dllexport) 声明。
对不起,人们。
我正在属于该项目的文件中定义StateConservator的代码。但是在我意识到在另一个文件中会更有意义之后。
我没有意识到第二个文件是另一个解决方案的一部分。因此,我没有编译第二个解决方案,因此出现了错误。
我想我需要Visual Studio的扩展,用另一种颜色为其他项目的选项卡着色。如果有人知道一个,我将不胜感激。
感谢您支持我造成的烦恼。
更新
请注意类后面缺少的 __declspec(dllexport) 声明。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++ 静态数据字段导致的使用者文件中的链接错误
- 生产者-使用者日志文件输出重复