Undefined symbols. ld: symbol not found

2019-02-19 03:09发布

问题:

Everything is working except this undefined symbols error:

bash-3.2$ make
g++ -Wall -g solvePlanningProblem.o Position.o AStarNode.o PRM.o PRMNode.o Worl.o SingleCircleWorld.o Myworld.o RECTANGLE.o CIRCLE.o -o solvePlanningProblem

`Undefined symbols:
  "Obstacle::~Obstacle()", referenced from:
      Myworld::~Myworld()in Myworld.o
      Myworld::~Myworld()in Myworld.o
      Myworld::~Myworld()in Myworld.o
  "RECTANGLE::RECTANGLE()", referenced from:
      Myworld::readObstacles(std::basic_istream<char, std::char_traits<char> >&
in Myworld.o
  "CIRCLE::CIRCLE()", referenced from:
      Myworld::readObstacles(std::basic_istream<char, std::char_traits<char> >&
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`

It's such a strange error. Is something wrong with the constructor or destructor? Any advice will help.

After adding {} after all constructors and destructors the error has been reduced to:

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

回答1:

The linker can't find the destructor for the Obstacle class.

Is it in another object file (perhaps Obstacle.o)? If so, add that to the list of objects to link.

Is it supposed to be an empty virtual destructor within the class definition? In that case, make sure you've written

virtual ~Obstacle() {}

and not

virtual ~Obstacle();

The first implements the destructor; the second declares that it exists, but is implemented somewhere else.



回答2:

Seems like you are missing the implementation of the desctructor ~Obstacle that is anyway defined..

LD is the linker, this means that everything compiles fine but when it starts to link binaries into one it can't find the destructor for Obstacle used in your code..

Add

~Obstacle() {}

to your class definition in .h file, or if you prefer just define it ~Obstacle() and provide implementation in .cpp file as ~Obstacle::Obstacle()



回答3:

It might be that you declared the D'tor but didn't implement it. Try to put {} in the .h file, or:

Obstacle::~Obstacle()
{
}

in the cpp.



回答4:

it also looks like the default constructors are missing from RECTANGLE and CIRCLE.



回答5:

You are missing a library. or have a broken tool chain (which depends on the include path for gcc).

Google turned up squat.. so clarifying what you are actually trying to build lets us help you more :)



标签: c++ linux linker