如何找到类的未定义的虚函数 [英] How to find undefined virtual functions of a classes

查看:236
本文介绍了如何找到类的未定义的虚函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个游戏从windows移植到mac。

I am porting a game from windows to mac.

这里我遇到了一些链接器错误。所有类似于

Here I am stuck with a bunch of linker errors. All errors similar to


未定义符号typeinfo for Baseclass,引用自:typeinfo Subclass Subclass.o vtable for Aclass从_ZTVNAclass引用:`

Undefined symbols "typeinfo for Baseclass", referenced from: typeinfo for Subclass Subclass.oor "vtable for Aclass referenced from _ZTVNAclass:`

我知道由于缺少虚拟定义

I know that the problem because of missing definition for virtual functions. Its big project and difficult to trace undefined virtual functions. Is their any way to quickly identify undefined virtual functions.

为什么编译器不能识别未定义的虚拟函数,并给出一个未定义的虚函数,

Why cant the compiler identify undefined virtual functions and give a meaningful message instead of cryptic vtable message.

最小范例: -

class Foo
{  
public:  
  virtual ~Foo(){};  
  virtual void  someMethod();  
};

int main()
{
    Foo  foo;
    return 0;
}

和g ++版本4.2.1(Apple Inc. build 5664)说: -

And g++ version 4.2.1 (Apple Inc. build 5664) says:-

Undefined symbols:  
  "vtable for Foo", referenced from:  
      Foo::Foo()   in ccj8yYI2.o  
ld: symbol(s) not found  
collect2: ld returned 1 exit status  


推荐答案

我相信这两个错误意味着类有虚拟函数,但没有非内联虚拟函数(如果你来自说Java的背景容易做)。 GCC发出vtable和(我相信)typeinfo,当它发出一个类中的第一个非内联虚拟函数。如果所有的虚拟函数都内联,那些位将不会生成。

I believe both of these errors mean that the class has virtual functions, but no non-inline virtual functions (easy to do if you come from say a Java background). GCC emits the vtable and (I believe) typeinfo when it emits the first non-inline virtual function in a class. If all your virtual functions are inlined those bits won't be generated.

您需要将一个内联虚函数移动到源文件,并使其非内联。析构函数通常是一个很好的候选者。

You'll need to move an inline virtual function to the source file and make it non-inline. The destructor is often a good candidate.

编辑:我想我误解了你的问题。暂时使虚拟函数非虚拟化,以使链接器为该精确函数而不是为vtable发出缺少的符号?你很明显不得不记得以后重新虚拟化它们。

I think I misread your question. What about temporarily making your virtual functions non-virtual so that the linker emits missing symbol for that exact function rather than for the vtable? You'd obviously have to remember to revirtualize them later.

EDIT2:
当我试图编译这个我有一个描述性的错误。你能粘贴一个不是最小的例子吗?

When I tried to compile this I got a descriptive error. Can you paste in a minimal example that doesn't?

class Foo
{
public:
    virtual ~Foo();
};

int main()
{
    Foo foo;

    return 0;
}

输出:

Undefined                       first referenced
 symbol                             in file
Foo::~Foo()                         /var/tmp//cc4C2j6B.o
vtable for Foo                      /var/tmp//cc4C2j6B.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

这篇关于如何找到类的未定义的虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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