Undefined reference to vtable, Qt in Linux
Asked Answered
W

5

5

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();
}
Waterhouse answered 24/12, 2010 at 11:20 Comment(0)
D
5

In your project.pro file add

QT += opengl

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

Dasha answered 24/12, 2010 at 11:40 Comment(3)
Thanks. It works now if I compile with QTCreator since I can directly edit the .pro file. But Code::Blocks somehow automatically creates the .pro file. Any ideas how I can manipulate it?Waterhouse
There might be an option inside C::B to import pro file, anyhow I suggest Qt Creator for Qt development.Dasha
Thanks, I will be definitely using Qt Creator.Waterhouse
L
3

Clean your project and run qmake on it.

Linskey answered 5/2, 2011 at 20:55 Comment(2)
You don't know how bitter-sweet this answer is after an hour of getting a vague error message. But thanks.Cahier
and for the record, once in a blue moon you end up having to do it twice...which is even an worse experience because you think something is really wrong with your code when there is NOTHING wrong with it at all...Philipps
C
1

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

Cheops answered 24/12, 2010 at 14:23 Comment(0)
N
1

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
Nicodemus answered 3/11, 2011 at 17:23 Comment(1)
Right click on your project root. Click "Run qmake". Fixed it for me. Strange this bug is still around? Is there a bug report?Symposiarch
C
1

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.

Chandelier answered 2/3, 2012 at 3:27 Comment(1)
Thanks, this solved it for me as well. I tried using "Clean All" from within Qt Creator, but that did nothing. I had to manually delete the folder.Incidental

© 2022 - 2024 — McMap. All rights reserved.