再次“未定义的符号”在运行时,c++
我有一个数据类型,我可以实例化该类型的变量。像这样:
FetchAddr faddr(VirtualMemoryAddress( 0x0a ));
FetchAdr 的定义是:
struct FetchAddr {
VirtualMemoryAddress theAddress;
FetchAddr(VirtualMemoryAddress anAddress)
: theAddress(anAddress)
{ }
};
现在我有一个类,faddr 是一个私有(或公共)变量
class FLEXUS_COMPONENT(BPred) {
static FetchAddr faddr;
public:
FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
: base( FLEXUS_PASS_CONSTRUCTOR_ARGS )
{
faddr = VirtualMemoryAddress( 0x0a );
}
...
}
假设宏定义正确。
代码编译和链接没有任何问题。然而,当我启动程序时,它说:
"undefined symbol: _ZN6nBPred14BPredComponent8faddr"
它说没有 faddr 的符号。
有什么想法吗?
I have a data type and I can instantiate a variable of that type. like this:
FetchAddr faddr(VirtualMemoryAddress( 0x0a ));
The definition of FetchAdr is:
struct FetchAddr {
VirtualMemoryAddress theAddress;
FetchAddr(VirtualMemoryAddress anAddress)
: theAddress(anAddress)
{ }
};
Now I have a class that faddr is a private (or public) variable
class FLEXUS_COMPONENT(BPred) {
static FetchAddr faddr;
public:
FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
: base( FLEXUS_PASS_CONSTRUCTOR_ARGS )
{
faddr = VirtualMemoryAddress( 0x0a );
}
...
}
Assume the macros are defined properly.
The code compiles and links without any problem. However when I start the program, it says:
"undefined symbol: _ZN6nBPred14BPredComponent8faddr"
it says there no symbol for faddr.
any idea about that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您声明静态成员时,您还必须在某个地方定义它,例如在 .cpp 文件中。并且还要记住链接到该文件。
问题 2 -
FetchAddr
没有默认构造函数。如果您需要将
faddr
作为类的静态成员,则还需要在定义时为其指定一个值,例如:创建一个共享的
faddr
由所有FLEXUS_COMPONENT(BPred)
对象。如果您希望每个对象都有自己的 faddr 变量副本,则可以将其设为非静态并在构造函数中对其进行初始化:
When you declare a static member you also have to define it somewhere, like in a .cpp file. And also remember to link to this file.
Problem number 2 -
FetchAddr
doesn't have a default constructor.If you need to have
faddr
as a static member of the class, you also need to give it a value when it is defined, like:That creates an
faddr
that is shared by allFLEXUS_COMPONENT(BPred)
objects.If you rather have it that each object has its own copy of the
faddr
variable, you can make it non-static and initialize it in the constructor:您必须在其他地方定义静态变量。
在单个 TU 中。
You must define the static variable elsewhere.
In a single TU.
尝试使用
-Wl,--no-undefined
进行编译,这样,如果库或任何其他依赖项中存在未定义的单个符号,链接器将拒绝完成链接。faddr
尚未正确链接,并且在没有看到更多程序的情况下,很难判断还发生了什么。Try compiling with the
-Wl,--no-undefined
so that the linker will refuse to complete the link if there is even a single symbol which is not defined in a library or any other dependencies.faddr
has not been properly linked and without seeing more of your program, it is hard to tell what else is going on.