简体   繁体   English

未定义的符号“ vtable for…”和“ typeinfo for ...”?

[英]Undefined symbols “vtable for …” and “typeinfo for…”?

Nearly the final step but still some strange erros.... 几乎是最后一步,但仍然有些奇怪。

bash-3.2$ make
g++ -Wall -c -g Myworld.cc
g++ -Wall -g solvePlanningProblem.o Position.o AStarNode.o PRM.o PRMNode.o World.o SingleCircleWorld.o Myworld.o RECTANGLE.o CIRCLE.o -o solvePlanningProblem
Undefined symbols:
  "vtable for Obstacle", referenced from:
      Obstacle::Obstacle()in Myworld.o
  "typeinfo for Obstacle", referenced from:
      typeinfo for RECTANGLEin RECTANGLE.o
      typeinfo for CIRCLEin CIRCLE.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [solvePlanningProblem] Error 1

What's the meaning of vtable and typeinfo? vtable和typeinfo是什么意思?

If Obstacle is an abstract base class, then make sure you declare all its virtual methods "pure virtual": 如果Obstacle是抽象的基类,请确保将其所有虚拟方法声明为“纯虚拟”:

virtual void Method() = 0;

The = 0 tells the compiler that this method must be overridden by a derived class, and might not have its own implementation. = 0告诉编译器该方法必须被派生类覆盖,并且可能没有自己的实现。

If the class contains any non-pure virtual functions, then the compiler will assume that they have an implementation somewhere, and its internal structures (vtable and typeinfo) might be generated in the same object file as one of those; 如果该类包含任何非纯虚函数,则编译器将假定它们在某处具有实现,并且其内部结构(vtable和typeinfo)可能与其中的一个在同一目标文件中生成; if those functions are not implemented, then the internal structures will be missing and you will get these errors. 如果未实现这些功能,则内部结构将丢失,并且您会得到这些错误。

The class Obstacle needs a virtual destructor. 障碍类需要一个虚拟的析构函数。 Change the destructor definition to be: 将析构函数定义更改为:

virtual ~Obstacle();

The definition of a destructor also creates the vtable for a class with virtual functions. 析构函数的定义还会为具有虚函数的类创建vtable。 It also ensures that a delete of a derived class instance through a base class pointer does the right thing. 它还确保通过基类指针删除派生类实例的操作正确。

(copy of my answer to question What should I do with this strange error? which seems to be a duplicate.) (我对问题的回答的副本, 这个奇怪的错误我应该怎么做?这似乎是重复的。)

Do you have an Obstacle.cc file? 您是否有Obstacle.cc文件? If so, you need to make sure it gets built into Obstacle.o , and that Obstacle.o gets added to the command line when you link your program. 如果是这样,则需要确保它内置在Obstacle.o ,并且在链接程序时将Obstacle.o添加到命令行中。

Also, make sure that you define all of the non-pure-virtual methods you declare. 另外,请确保定义所有声明的非纯虚拟方法。 If you declare a pure virtual destructor, you need to define that too. 如果声明纯虚拟析构函数,则也需要定义它。

vtable and typeinfo are internal structures generated by the C++ compiler. vtable和typeinfo是C ++编译器生成的内部结构。 vtable is used for calling virtuable functions and typeinfo is used for RTTI. vtable用于调用virtuable函数,typeinfo用于RTTI。

Different compilers have different strategies for when they generate these structures. 不同的编译器在生成这些结构时具有不同的策略。 One strategy I've seen is they will generate the table in the same object file that contains the first virtual function in the class. 我见过的一种策略是,它们将在包含该类中第一个虚拟函数的同一目标文件中生成表。

There is another reason you can get this error, and just want to document it here. 还有一个原因会导致您收到此错误,并且只想在此处进行记录。 I was linking with a static library which did not have RTTI. 我正在与没有RTTI的静态库链接。 So Using the C++ flag -fno-rtti fixed for me. 因此,使用C ++标志-fno-rtti为我修复了。 If you do not need RTTI, you can using this flag as well. 如果不需要RTTI,则也可以使用此标志。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM