Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can't compile simple source code working with OpenGL



  • Hi, please help.

    I can't compile simple source code working with OpenGL, taking from "Qt 5.3 Professional programming with C++" book (author Max Schlee).

    Every time when I build the source code I get the following errors:

    • D:\c++_project\build-OGLQuad-Desktop_Qt_5_5_1_MinGW_32bit-Debug\debug\OGLQuad.o:-1: In function `ZN7OGLQuad8resizeGLEii':
    • D:\c++_project\test9\OGLQuad.cpp:37: error: undefined reference to `_imp__glMatrixMode@4'
    • D:\c++_project\test9\OGLQuad.cpp:38: error: undefined reference to `_imp__glLoadIdentity@0'
    • D:\c++_project\test9\OGLQuad.cpp:39: error: undefined reference to `_imp__glViewport@16'
    • D:\c++_project\test9\OGLQuad.cpp:40: error: undefined reference to `_imp__glOrtho@48'
    • D:\c++_project\test9\OGLQuad.cpp:46: error: undefined reference to `_imp__glClear@4'
    • D:\c++_project\test9\OGLQuad.cpp:48: error: undefined reference to `_imp__glBegin@4']([link url]([link url](link url)))
      etc

    I use Qt 5.5.1, Qt Creator 3.6, MinGW 4.9.3 (32bit)

    My environment:
    OS Windows 7, 64bit.

    Source code of my project:

    pro file.

    TEMPLATE = app
    QT += widgets opengl core gui
    HEADERS = OGLQuad.h
    SOURCES = OGLQuad.cpp
    main.cpp
    windows:TARGET = ../OGLQuad

    main.cpp

    #include <QApplication>
    #include "OGLQuad.h"
    
    // ----------------------------------------------------------------------
    int main(int argc, char** argv)
    {
        QApplication app(argc, argv);
        OGLQuad      oglQuad;
    
        oglQuad.resize(200, 200);
        oglQuad.show();
    
        return app.exec();
    }
    

    OGLQuad.h

    #pragma once
    
    #include <QGLWidget>
    
    class OGLQuad : public QGLWidget {
    protected:
        virtual void initializeGL(                       );
        virtual void resizeGL    (int nWidth, int nHeight);
        virtual void paintGL     (                       );
    
    public:
        OGLQuad(QWidget* pwgt = 0);
    };
    

    OGLQuad.cpp

    #include "OGLQuad.h"
    
    // ----------------------------------------------------------------------
    OGLQuad::OGLQuad(QWidget* pwgt/*= 0*/) : QGLWidget(pwgt) 
    {
    }
    
    // ----------------------------------------------------------------------
    /*virtual*/void OGLQuad::initializeGL()
    {
        qglClearColor(Qt::black);
    }
    
    // ----------------------------------------------------------------------
    /*virtual*/void OGLQuad::resizeGL(int nWidth, int nHeight)
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glViewport(0, 0, (GLint)nWidth, (GLint)nHeight);
        glOrtho(0, 100, 100, 0, -1, 1); 
    }
    
    // ----------------------------------------------------------------------
    /*virtual*/void OGLQuad::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glBegin(GL_QUADS);
            glColor3f(1, 0, 0); 
            glVertex2f(0, 100);
    
            glColor3f(0, 1, 0);
            glVertex2f(100, 100);
    
            glColor3f(0, 0, 1);
            glVertex2f(100, 0);
    
            glColor3f(1, 1, 1);
            glVertex2f(0, 0);
        glEnd();
    }
    

    Thanks



  • @Zholaman For those who faced with this problem.

    I have resolved it by adding the following lib in my pro file:

    LIBS += -LD:\Qt\5.5\mingw492_32\lib\libQt5OpenGL.a -lopengl32

    The D:\Qt\5.5\mingw492_32\lib\libQt5OpenGL.a is path to lib file (libQt5OpenGL.a)


Log in to reply