先贴出错误:
../libs/******/libsp***.so: undefined reference to `typeinfo for Base'
../libs******/libsp***.so: undefined reference to `Base::~Base()'
../libs******/libsp***.so: undefined reference to `vtable for Base'
可用如下实例进一步说明:
g++ -o polygon shape.o circle.o square.o polygon.o
circle.o(.gnu.linkonce.r._ZTI6Circle+0x8): undefined reference to `typeinfo for Shape'
square.o(.gnu.linkonce.r._ZTI6Square+0x8): undefined reference to `typeinfo for Shape'
polygon.o(.gnu.linkonce.t._ZN5ShapeC2Ev+0x8): In function `Shape::Shape()':
: undefined reference to `vtable for Shape'
collect2: ld returned 1 exit status
And you can be scratching you head for hour over that one!
The error? shape.o contains a base class from which classes are derived in circle.o and square.o .. but virtual function(s) in shape's definition are missing null bodies.
解决方法:
The fix? You've got line(s) like
virtual float getarea() ;
that should read
virtual float getarea() {} ;
按照文中所说稍微修改了一下,在析构函数后面添加了{},再make,成功了,高兴啊!问题终于解决了。我的所有虚函数都是有定义的,没想到就因为写基类 的这个虚析构函数大意,没写函数体就出现了一个困扰我几天的莫名其妙的错误。就virtual ~Base();和virtual ~Base() {};的区别,编译可以通过却搞出个莫名其妙的链接错误。链接器linker需要将虚函数表vtable 放入某个object file,但是linker无法找到正确的object文件。这个错误常见于刚刚创建一系列有继承关系的class的时候,这个时候很容易忘了给base class的virtual function加上函数实现。解决办法:给基类的virtual函数加上本来就应该有的function body。当含有虚函数的类未将析构函数声明为virtual时也会出现这个链接错误