Mores Fre
Mores Fre

Reputation: 51

Undefined reference to vtable, Qt in Linux

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();
}

Upvotes: 5

Views: 5475

Answers (5)

Andy Jones
Andy Jones

Reputation: 11

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.

Upvotes: 1

Wes
Wes

Reputation: 5067

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

Upvotes: 1

CoutPotato
CoutPotato

Reputation: 535

Clean your project and run qmake on it.

Upvotes: 3

AProgrammer
AProgrammer

Reputation: 52294

'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).

Upvotes: 1

ismail
ismail

Reputation: 47602

In your project.pro file add

QT += opengl

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

Upvotes: 5

Related Questions