Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 256
Mores Fre Avatar asked Dec 24 '10 11:12

Mores Fre


People also ask

What does undefined reference to Vtable mean?

In summary, there are three key causes of the "undefined reference to vtable" error: A member function is missing its definition. An object file is not being linked. All virtual functions have inline definitions.

How do you fix undefined references in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

What is undefined reference?

An “Undefined Reference” error occurs when we have a reference to object name (class, function, variable, etc.) in our program and the linker cannot find its definition when it tries to search for it in all the linked object files and libraries.


3 Answers

In your project.pro file add

QT += opengl

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

like image 104
ismail Avatar answered Oct 07 '22 14:10

ismail


Clean your project and run qmake on it.

like image 42
CoutPotato Avatar answered Oct 07 '22 16:10

CoutPotato


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

like image 30
AProgrammer Avatar answered Oct 07 '22 14:10

AProgrammer