C++ 未定义的符号“vtable for ...”和“typeinfo for...”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1693634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined symbols "vtable for ..." and "typeinfo for..."?
提问by Lisa
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 的含义是什么?
回答by Mike Seymour
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; if those functions are not implemented, then the internal structures will be missing and you will get these errors.
如果该类包含任何非纯虚函数,那么编译器将假定它们在某处具有实现,并且其内部结构(vtable 和 typeinfo)可能在与其中之一相同的目标文件中生成;如果未实现这些功能,则内部结构将丢失,您将收到这些错误。
回答by janm
The class Obstacle needs a virtual destructor. Change the destructor definition to be:
Obstacle 类需要一个虚拟析构函数。将析构函数定义更改为:
virtual ~Obstacle();
The definition of a destructor also creates the vtable for a class with virtual functions. It also ensures that a delete of a derived class instance through a base class pointer does the right thing.
析构函数的定义还为具有虚函数的类创建了 vtable。它还确保通过基类指针删除派生类实例是正确的。
(copy of my answer to question What should I do with this strange error?which seems to be a duplicate.)
(我对问题的回答副本我应该如何处理这个奇怪的错误?这似乎是重复的。)
回答by vine'th
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. So Using the C++ flag -fno-rtti
fixed for me. If you do not need RTTI, you can using this flag as well. Hope this helps.
您可能会收到此错误还有另一个原因,只是想在此处记录下来。我正在链接一个没有 RTTI 的静态库。所以使用-fno-rtti
为我修复的 C++ 标志。如果您不需要 RTTI,也可以使用此标志。希望这可以帮助。
回答by bk1e
Do you have an Obstacle.cc
file? 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.cc
文件吗?如果是这样,您需要确保它被内置到 中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.
此外,请确保您定义了您声明的所有非纯虚拟方法。如果你声明了一个纯虚析构函数,你也需要定义它。
回答by R Samuel Klatchko
vtable and typeinfo are internal structures generated by the C++ compiler. vtable is used for calling virtuable functions and typeinfo is used for RTTI.
vtable 和 typeinfo 是由 C++ 编译器生成的内部结构。vtable 用于调用虚拟函数,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.
不同的编译器在生成这些结构时有不同的策略。我见过的一个策略是它们将在包含类中第一个虚函数的同一个对象文件中生成表。