【发布时间】:2016-06-26 11:16:23
【问题描述】:
我收到以下代码的重定位错误。请有人帮我解决这个问题。
A.hpp
namespace
{
class A
{
public:
A(const std::string&);
//Few Get() methods
private:
//Some private stuff
};
extern A objA;
}
A.cpp
#include<A.hpp>
const std::string str("FIXED STRING HERE");
A objA(str);
A::A(const std::string& Istring)
{
//Some data processing here
}
ClientCode.cpp
#include<A.hpp>
// Want to access A::get() here using objA;
objA.Get();
我收到以下错误:
重定位R_X86_64_PC32 对未定义符号`(匿名命名空间)::str' 在制作共享对象时不能使用;使用 -fPIC 重新编译
最终链接失败:错误值
collect2: ld 返回 1 个退出状态
另外,请有人知道这个外部对象的内存是什么时候分配的。 我的主要目的是只拥有 A 类的单个对象,并在我包含 A.hpp 时在许多文件中使用它
【问题讨论】:
标签: c++