Undefined reference to vtable, Qt in Linux

浪尽此生 提交于 2019-12-01 17:40:01

问题


I was trying to compile a Qt and OpenGL program under Code::Blocks in Ubuntu 10.04. I get the 'undefined reference to 'vtable for GLWidget'

#ifndef _GLWIDGET_H
#define _GLWIDGET_H

#include <QtOpenGL/QGLWidget>
#include "stdlib.h"

class GLWidget : public QGLWidget {

    Q_OBJECT // must include this if you use Qt signals/slots

public:
    GLWidget(QWidget *parent = 0);
    ~GLWidget();
protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
    void keyPressEvent(QKeyEvent *event);
};

#endif  /* _GLWIDGET_H */

I borrowed the code from this guy to see if it works, because mine wasn't working because of the same reason. Code

And here is the GLWidget.cpp:

#include <QtGui/QMouseEvent>
#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {
    setMouseTracking(true);
}

GLWidget::~GLWidget()
{
}

void GLWidget::initializeGL() {
   ...
}

void GLWidget::resizeGL(int w, int h) {
   ...
}

void GLWidget::paintGL() {
    ...
}

void GLWidget::keyPressEvent(QKeyEvent* event) {
    ...
    }
}

I removed the code from the GL part to keep it shorter. Should you need it, I can always post it up.

#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>
#include "glwidget.h"

int main(int argc, char *argv[]) {

    QApplication app(argc, argv);

    GLWidget window;
    window.resize(800,600);
    window.show();

    return app.exec();
}

回答1:


In your project.pro file add

QT += opengl

So it knows that it has to link to GL libraries.




回答2:


Clean your project and run qmake on it.




回答3:


'undefined reference to 'vtable for GLWidget' most probably means that the definition of the first non inline virtual function of GLWidget isn't linked in the executable.

In the present case, my guess it is that it should be provided by the file generated by moc (but as I don't program for QT, I may be mistaken here).




回答4:


This happens sometimes when adding Q_OBJECT to a header file and can mean a missing moc_ file.

I have found from personal experience doing the following has resolved the issue:

  1. $ qmake filename.pro
  2. $ make



回答5:


I had this exact problem after adding Q_OBJECT to one of the header files in my project.

I was only getting this error message from within QT Creator, and not when I built my project from the Linux command line.

For me, the solution was to delete the YourProjectName-build-desktop folder which resides on the same level as your project directory. Then when I built the project from within QT Creator, it magically worked.



来源:https://stackoverflow.com/questions/4526016/undefined-reference-to-vtable-qt-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!