我只是遇到了以下错误:

(.gnu.linkonce。[内容]):定义 引用[方法][对象] 文件:(.gnu.linkonce。[内容]): 对' typeinfo for '的未定义引用 (名称)的

为什么可能会得到这些“未定义的引用typeinfo”链接错误之一? 有人能解释一下幕后发生了什么吗?


当前回答

在我的例子中,它是一个接口类中的虚函数,没有定义为纯虚函数。

class IInterface
{
public:
  virtual void Foo() = 0;
}

我忘记了= 0位。

其他回答

有了这个错误消息,g++的链接器是在告诉你,当需要时,它无法为给定的类组装完整的静态typeinfo描述符。正如许多人已经指出的那样,这很可能是由于缺少虚函数的定义。

但不好的是,错误消息的顺序可能与直觉相反,“对typeinfo的未定义引用”出现在对缺失的虚拟定义的未定义引用之前。这里有一个例子,我刚刚经历过:

/usr/bin/ld: module.o:(.data.rel.ro+0x10): undefined reference to `typeinfo for type_xxx'
/usr/bin/ld: module.o:(.data.rel.ro+0x28): undefined reference to `typeinfo for type_xxx'
/usr/bin/ld: module.o:(.data.rel.ro+0x40): undefined reference to `typeinfo for type_xxx'
/usr/bin/ld: module.o:(.data.rel.ro+0x150): undefined reference to `type_xxx::has_property(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

因此,type_xxx::has_property(const std::string&)的缺失定义仅作为第四个错误报告。因此,有时跳过那些不理解的错误消息,首先处理那些容易理解的错误消息是有好处的。因为在这种情况下,添加缺失的定义也会修复未定义typeinfo引用的问题。

I encounter an situation that is rare, but this may help other friends in similar situation. I have to work on an older system with gcc 4.4.7. I have to compile code with c++11 or above support, so I build the latest version of gcc 5.3.0. When building my code and linking to the dependencies if the dependency is build with older compiler, then I got 'undefined reference to' error even though I clearly defined the linking path with -L/path/to/lib -llibname. Some packages such as boost and projects build with cmake usually has a tendency to use the older compiler, and they usually cause such problems. You have to go a long way to make sure they use the newer compiler.

当混合使用-fno-rtti和-frtti代码时也会发生这种情况。然后,您需要确保在-frtti代码中访问type_info的任何类都使用-frtti编译了它们的key方法。这种访问可以发生在创建类的对象时,使用dynamic_cast等。

(来源)

我在这个错误上花了几个小时,虽然这里的其他答案帮助我理解发生了什么,但它们并没有解决我的特定问题。

我正在开发一个使用clang++和g++进行编译的项目。我没有使用clang++的链接问题,但得到了未定义的引用'typeinfo错误与g++。

重点是:在g++中,连接顺序很重要。如果你以不正确的顺序列出你想要链接的库,你会得到typeinfo错误。

有关与gcc/g++链接顺序的更多细节,请参阅这个SO问题。

当声明的(非纯)虚函数缺少主体时,就会发生这种情况。在你的类定义中,如下所示:

virtual void foo();

应该定义(内联或链接源文件中):

virtual void foo() {}

或声明为纯虚拟的:

virtual void foo() = 0;