我只是遇到了以下错误:
(.gnu.linkonce。[内容]):定义 引用[方法][对象] 文件:(.gnu.linkonce。[内容]): 对' typeinfo for '的未定义引用 (名称)的
为什么可能会得到这些“未定义的引用typeinfo”链接错误之一? 有人能解释一下幕后发生了什么吗?
我只是遇到了以下错误:
(.gnu.linkonce。[内容]):定义 引用[方法][对象] 文件:(.gnu.linkonce。[内容]): 对' typeinfo for '的未定义引用 (名称)的
为什么可能会得到这些“未定义的引用typeinfo”链接错误之一? 有人能解释一下幕后发生了什么吗?
当前回答
前面的答案是正确的,但是这个错误也可能是由于试图对没有虚函数的类的对象使用typeid而导致的。c++ RTTI需要虚表,因此希望对其执行类型标识的类至少需要一个虚函数。
如果希望类型信息作用于一个实际上不需要任何虚函数的类,可以将析构函数设为虚函数。
其他回答
在我的例子中,它是一个接口类中的虚函数,没有定义为纯虚函数。
class IInterface
{
public:
virtual void Foo() = 0;
}
我忘记了= 0位。
当声明的(非纯)虚函数缺少主体时,就会发生这种情况。在你的类定义中,如下所示:
virtual void foo();
应该定义(内联或链接源文件中):
virtual void foo() {}
或声明为纯虚拟的:
virtual void foo() = 0;
我有同样的错误时,我的接口(与所有纯虚函数)需要一个更多的函数,我忘记“空”它。
我有
class ICommProvider { public: /** * @brief If connection is established, it sends the message into the server. * @param[in] msg - message to be send * @return 0 if success, error otherwise */ virtual int vaSend(const std::string &msg) = 0; /** * @brief If connection is established, it is waiting will server response back. * @param[out] msg is the message received from server * @return 0 if success, error otherwise */ virtual int vaReceive(std::string &msg) = 0; virtual int vaSendRaw(const char *buff, int bufflen) = 0; virtual int vaReceiveRaw(char *buff, int bufflen) = 0; /** * @bief Closes current connection (if needed) after serving * @return 0 if success, error otherwise */ virtual int vaClose(); };
Last vaClose不是虚拟的,所以编译时不知道从哪里获得它的实现,因此感到困惑。我传达的信息是:
…TCPClient.o:(.rodata+0x38): undefined reference to ' typeinfo for ICommProvider'
简单的更改
virtual int vaClose();
to
virtual int vaClose() = 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引用的问题。
一个可能的原因是声明了虚函数而没有定义它。
当你声明它而没有在同一个编译单元中定义它时,你就表明它在其他地方定义了——这意味着链接器阶段将尝试在其他编译单元(或库)中找到它。
定义虚函数的一个例子是:
virtual void fn() { /* insert code here */ }
在本例中,您将定义附加到声明中,这意味着链接器稍后不需要解析它。
这条线
virtual void fn();
声明fn()而不定义它,这将导致您所要求的错误消息。
它与代码非常相似:
extern int i;
int *pi = &i;
它声明整数I在另一个编译单元中声明,必须在链接时解析(否则PI不能设置为它的地址)。