7

I tried to build a simple OpenGL App with Qt 5.5.1, and everything is fine up until I try to use openGL native function calls like glClearColor.

The Widget actually compiles and produces a black screen, but after I try to use any openGL native function it doesn't even link, but produces the error:

glwidget.cpp:10: error: undefined reference to '_imp__glClearColor@16'

Here is the .pro file:

            QT       += core gui opengl

            CONFIG   += windows

            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

            TARGET = Vehicle_simulation

            TEMPLATE = app

            SOURCES += main.cpp\
                    simulation.cpp \
                glwidget.cpp

            HEADERS  += simulation.h \
                glwidget.h

            FORMS    += simulation.ui

I'm using Desktop Qt mingw492_32 kit. The strange thing though is that I did not find libQtOpenGL.so in the lib folder. Is my QT installation faulty? I tried to reinstall it multiple times, but it didn't help. Where can I download that specific library?

linking it to the project would solve the problem, but I can't seem to find it anywhere.

The problem is the openGL module is missing from the QT installation, it's not that I am unable to link to it.

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46
  • @genpfault , not to be picky, but the duplicate suggestion is wrong. I found the source of the problem and it's because QT by default builds with ANGLE, which only supports openGL ES, not desktop openGL. To solve this I have to do a custom build of QT Core with openGL in it. I'd be more, than glad to answer the question if someone wouléd encounter this problem. – Dávid Tóth Nov 06 '15 at 11:14
  • After opening an example openGL project which compiled and linked, I found the source of the problem, it is not a linking error, I used the wrong implementation of GLWIdget, I will post my answer as soon as I can compile enough thought to it. – Dávid Tóth Nov 06 '15 at 12:29

1 Answers1

9

After I opened up an example program by chance, just to take a look at the implementation; I found that the same QOpenGLWidget has been implemented in a new example program.

After a bit of analysis I managed to figure out the problem.

QT has an internal openGL implementation and native openGL support as well. The widget I used inherited from QOpenGLWidget, but the openGL function calls (like glClearColor) tried to access native openGL implementation in Qt. which Since those were not included in my Qt build, the project would not build.

To fix this one has to either run a custom Qt core build, or use the openGL wrapper provided by Qt.

I used the Qt wrapper in the end, which was also used by the example program Cube. the class used to implement the widget (called GLWidget in my implementation) has to inherit not only from QOpenGLWidget, but also from QOpenGLFunctions. The latter part was missing from my implementation. The source code for it is the following:

glwidget.h (initial):

#ifndef GLWIDGET_H
#define GLWIDGET_H


#include <QOpenGLFunctions>
#include <QOpenGLWidget>

class GLWidget : public QOpenGLWidget
{
    Q_OBJECT

public:
    explicit GLWidget(QWidget *parent);

protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int w, int h);
};

#endif // GLWIDGET_H

glwidget.h (fixed):

#ifndef GLWIDGET_H
#define GLWIDGET_H


#include <QOpenGLFunctions>
#include <QOpenGLWidget>

class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    explicit GLWidget(QWidget *parent);

protected:
    void initializeGL() Q_DECL_OVERRIDE;
    void paintGL() Q_DECL_OVERRIDE;
    void resizeGL(int w, int h) Q_DECL_OVERRIDE;
};

#endif // GLWIDGET_H

Be careful!

Before any gl* functions are used the function initializeOpenGLFunctions(); has to be called, otherwise a very cryptic run-time error will pop up. i.e.:

void GLWidget::initializeGL(){
    initializeOpenGLFunctions();
    glClearColor(1,0,0,1);
}

Hope this will help someone else as well.

Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46
  • thanks. it helped. please consider adding reference source as well. – Mohammad Tauqir Aug 11 '16 at 05:53
  • Glad to be of help! One reference would be the following question: http://stackoverflow.com/questions/31688370/qt-5-5-with-qmake-linker-cannot-resolve-opengl-function-calls - From there I was curious to see the classes it used, looked into the source, and tried various attempts until it worked. – Dávid Tóth Aug 18 '16 at 09:15
  • 1
    versions of qt prior to 5 were including export library for opengl into build, qt5 does own wrapper that is built by principle rightness over performance, so you call class methods or wrappers rather library functions. there is virtue in both approaches – Swift - Friday Pie Dec 29 '16 at 11:43