c++qtopenglqglwidget

Qt OpenGL error '_imp_gl...'


Trying to learn how to use QGLWidget, but already so much time wasted handling these errors. I dont even understand them. I googled, but nothing. Please, any suggestions?

Code: https://gist.github.com/anonymous/77c57fde631c77810775

In function `ZN6Widget12initializeGLEv':
 undefined reference to `_imp__glEnable@4'
 undefined reference to `_imp__glShadeModel@4'
 and more...

Solution

  • You're not linking against any GL library.

    Since Qt 5.5, on Windows, by default, Qt does not link against libGL, but has a runtime mechanism for deciding to load either libGL or ANGLE (depending on your OS capabilities). This means you can't just use glFoo -- you will get linking errors.


    Solution: go through function resolvers, for instance QOpenGLFunctions:

    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    f->glEnable(GL_FOOBAR);
    f->glDrawArrays(...);
    

    Note that on Windows you need function resolvers anyhow to use any OpenGL function introduced after 1.1.

    Also, if you're using ES2-only or Desktop GL-only functions, you should force Qt to load the right GL implementation:

    int main(int argc, char **argv) {
        // *before* creating it
        QApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
        QApplication app(argc, argv); 
    

    More info here.

    (More radical solution: recompile Qt passing -opengl desktop or -opengl es2 to configure).