g ++未定义对typeinfo的引用 [英] g++ undefined reference to typeinfo

查看:1394
本文介绍了g ++未定义对typeinfo的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是遇到以下错误(并在网上找到解决方案,但它不存在于Stack Overflow):


gnu.linkonce。[stuff]):undefined
引用[method] [object
file] :(。gnu.linkonce。[stuff]):
未定义引用`typeinfo for $为什么可能得到这些未定义的对typeinfo的引用链接器错误之一?

为什么可能得到这些未定义的引用typeinfo链接器错误之一



$ b <

(如果您可以解释幕后情况,则为红利点。)

解决方案

一个可能的原因是因为你在不定义虚函数的情况下声明它。



当你在同一个编译单元中声明它时,定义在其他地方 - 这意味着链接器阶段将尝试在其他编译单元(或库)中找到它。



定义虚函数的一个示例是:

  virtual void fn(){/ *在此插入代码* /} 

在这种情况下,您要为声明附加一个定义,这意味着链接器以后不需要解析。



  virtual void fn 

声明 fn()



它与代码非常相似:

 

code> extern int i;
int * pi =& i;

其中声明了 i 在另一个编译单元中,必须在链接时解析(否则 pi 不能设置为它的地址)。


I just ran across the following error (and found the solution online, but it's not present in Stack Overflow):

(.gnu.linkonce.[stuff]): undefined reference to [method] [object file]:(.gnu.linkonce.[stuff]): undefined reference to `typeinfo for [classname]'

Why might one get one of these "undefined reference to typeinfo" linker errors?

(Bonus points if you can explain what's going on behind the scenes.)

解决方案

One possible reason is because you are declaring a virtual function without defining it.

When you declare it without defining it in the same compilation unit, you're indicating that it's defined somewhere else - this means the linker phase will try to find it in one of the other compilation units (or libraries).

An example of defining the virtual function is:

virtual void fn() { /* insert code here */ }

In this case, you are attaching a definition to the declaration, which means the linker doesn't need to resolve it later.

The line

virtual void fn();

declares fn() without defining it and will cause the error message you asked about.

It's very similar to the code:

extern int i;
int *pi = &i;

which states that the integer i is declared in another compilation unit which must be resolved at link time (otherwise pi can't be set to it's address).

这篇关于g ++未定义对typeinfo的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆