如何访问静态原子变量而不会导致链接器错误?访问静态原子数据成员会导致链接器错误
我已经减少了我的代码示例如下:
#include <iostream>
#include <atomic>
class MyClass
{
public:
static std::atomic_bool onOrOff;
};
std::atomic_bool onOrOff(false);
int main(int argc, const char * argv[]) {
std::cout << "It is: " << (MyClass::onOrOff? "on": "off") << "\n";
return 0;
}
这导致下面的链接错误(MAXOS X 10.11,7.2的XCode):
Undefined symbols for architecture x86_64:
"MyClass::onOrOff", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
NB:没有如果我删除main()中的行访问 MyClass::onOrOff
,那么构建错误,所以我认为这排除了编译器选项的问题。
真棒,谢谢。我会给自己的哦!当天奖励:-) –