Undefined symbols "vtable for ..." and "typeinfo for..."?

C++Linker ErrorsUndefined ReferenceVtablePure Virtual

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

If Obstacle is an abstract base class, then make sure you declare all its virtual methods "pure virtual":

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.

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.

Solution 2 - C++

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. 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 https://stackoverflow.com/questions/1694785/what-should-i-do-with-this-strange-error which seems to be a duplicate.)

Solution 3 - C++

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.

Solution 4 - C++

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.

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.

Solution 5 - C++

vtable and typeinfo are internal structures generated by the C++ compiler. vtable is used for calling virtuable functions and typeinfo is used for 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLisaView Question on Stackoverflow
Solution 1 - C++Mike SeymourView Answer on Stackoverflow
Solution 2 - C++janmView Answer on Stackoverflow
Solution 3 - C++vine'thView Answer on Stackoverflow
Solution 4 - C++bk1eView Answer on Stackoverflow
Solution 5 - C++R Samuel KlatchkoView Answer on Stackoverflow