最近用Qt写一个项目时出现了undefined reference to 'vtable for class::methode'的错误!
一 描述
程序结构如下:
共享库: libyqmath.so 有个类CMyDraw --完成一些绘画操作
1. 编译成功,存放在../../../lib/中,即../../../lib/中有libyqmath.so libyqmath.so.1 ...
GUI应用程序:demo
1. 调用CMyDraw进行绘画
2. 在.pro文件中加入了: LIBS += -L../../../li/libyqmath.so
INCLUDEPATH += . ./../../../include ./../../../lib
make时出现如下错误信息:
g++ -Wl,-O1 -o ../../../bin/demo build/cmydrawdlg.o build/cmymathdlg.o build/main.o build/moc_cmydrawdlg.o build/moc_cmymathdlg.o -L/usr/lib -L../../../bin/libyqmath.so -lQtGui -lQtCore -lpthread
build/cmydrawdlg.o: In function `CMyDrawDlg::CMyDrawDlg()':
/home/yangjingtu/projects/study/qt/code/demo/src/cmydrawdlg.cpp:35: undefined reference to `CMyDraw::CMyDraw()'
build/cmydrawdlg.o: In function `~CMyDraw':
/home/yangjingtu/projects/study/qt/code/demo/src/../../yqmath/src/cmydraw.h:36: undefined reference to `vtable for CMyDraw
/home/yangjingtu/projects/study/qt/code/demo/src/../../yqmath/src/cmydraw.h:36: undefined reference to `vtable for CMyDraw
build/cmydrawdlg.o: In function `CMyDrawDlg::MyDraw()':
/home/yangjingtu/projects/study/qt/code/demo/src/cmydrawdlg.cpp:70: undefined reference to `CMyDraw::DrawMyRect(QPicture*, int, int, int, int)'
build/main.o: In function `~CMyDraw':
/home/yangjingtu/projects/study/qt/code/demo/src/../../yqmath/src/cmydraw.h:36: undefined reference to `vtable for CMyDraw
/home/yangjingtu/projects/study/qt/code/demo/src/../../yqmath/src/cmydraw.h:36: undefined reference to `vtable for CMyDraw'
/home/yangjingtu/projects/study/qt/code/demo/src/../../yqmath/src/cmydraw.h:36: undefined reference to `vtable for CMyDraw'
/home/yangjingtu/projects/study/qt/code/demo/src/../../yqmath/src/cmydraw.h:36: undefined reference to `vtable for CMyDraw'
build/cmymathdlg.o: In function `CMyMathDlg::intadd()':
/home/yangjingtu/projects/study/qt/code/demo/src/cmymathdlg.cpp:54: undefined reference to `MyMathInt::add(int, int)'
collect2: 错误:ld 返回 1
二 原因
百度,google了一大圈基本分为两大类:
1. 用了Q_OBJECT的宏,但没有包含其moc文件或没有加到链接列表中去。
2. 所依赖的库文件问题。
排除1,因为共享库已生成成功,那原因就是库或其路径问题。
三 解决方案
尝试1: 更改.pro文件
LIBS += -lyqmath
qmake make后出现: /usr/bin/ld: cannot find -lyqmath
改回
LIBS += -L../../../lin/libyqmath.so
不会出现cannot find问题,那说明路径是对的,程序也找到相应该的库
尝试2: 在/etc/ld.so.conf.d/下添加my-project.conf文件并把路径加进去
sudo vim /ect/ld.so.conf.d/my-project.conf
> .../project/study/qt/lib
sudo ldcofig
再尝试1的方法,问题依旧
尝试3: 更改.pro文件
LIBS += -L../../../lib -lyqmath
qmake make后:太帅了成功了!
一定要注意LIBS的写法!!!!
欢迎转载本文,请注意作者和出处!