Dimitris jim
Dimitris jim

Reputation: 33

Undefined reference to glew methods in a Qt project

I have a serious problem when using opengl in Qt 5.1.0. My compiler is MinGW 32 bit and I use Windows10 64 bit. I am trying to create a main window containing an OpenGL widget. In this OpenGL widget, I am trying to draw a triangle.

I have downloaded glew1.9.0 (because glew-2.0.0 download gives me an empty lib folder). I have included the static libraries glew32s.lib (not the dynamic .dll). I have added #define GLEW_STATIC and #include <gl/glew.h>. I have also added the lib path and the information about libraries to link against as well as the include path in my .pro file.

But still I am getting undefined reference for all functions included in glew.h. Actually I have noticed that although I am using e.g. glGenBuffers(), I get a message for __glewGenBuffers(). Inside gl.glew.h, I found out that line where glGenBuffers is defined, calls__glewGenBuffers` which is not yet defined.

1685    #define glGenBuffers GLEW_GET_FUN(__glewGenBuffers)
14376   GLEW_FUN_EXPORT PFNGLGENBUFFERSPROC __glewGenBuffers;

So, could the problem be that __glewGenBuffers is called but not defined yet in glew.h? I mean, am I missing somehting here? I am trying for 2 days now, I have tried everything posted in similar posts but i cannot find the solution.

Here is a small but representative part of the code:

glwidget.cpp:

#define GLEW_STATIC
#include <gl/glew.h>
#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
{

}

extern "C" void GLWidget::initializeGL()
{
    glewInit();
    glClearColor(0, 1, 0, 1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
    GLfloat verts[] =
    {
        +0.0f, +1.0f,
        -1.0f, -1.0f,
        +1.0f, -1.0f,
    };
    GLuint myBufferID;
    glGenBuffers(1, &myBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, myBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

}

void GLWidget::resizeGL(int w, int h)
{
}

void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

.pro file:

QT       += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = New_Beam
TEMPLATE = app


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

HEADERS  += mainwindow.h \
    glwidget.h \
    ../../../../../Qt/Qt5.3.2/glew-1.9.0/include/GL/glew.h

FORMS    += mainwindow.ui

win32: LIBS += -L"C:/Qt/Qt5.3.2/glew-1.9.0/lib" -lglew32s -lopengl32

INCLUDEPATH += C:/Qt/Qt5.3.2/glew-1.9.0/include
DEPENDPATH += C:/Qt/Qt5.3.2/glew-1.9.0/include

The message is:

undefined reference to '__glewGenBuffers'

Same for all glew functions I have used. Any answer is welcome.

Upvotes: 3

Views: 1291

Answers (1)

rocambille
rocambille

Reputation: 15956

If you are using Qt, you should not use glew: at best it will be useless since Qt provides encapsulations for OpenGL functions. So use Qt classes like QOpenGLBuffer or QOpenGLFunctions if you really want to do the work "manually":

class GLWidget : public QGLWidget, protected QOpenGLFunctions
{
    // ...
};

And the implementation:

#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
{

}

void GLWidget::initializeGL()
{
    initializeOpenGLFunctions();
    glClearColor(0, 1, 0, 1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
    GLfloat verts[] =
    {
        +0.0f, +1.0f,
        -1.0f, -1.0f,
        +1.0f, -1.0f,
    };
    GLuint myBufferID;
    glGenBuffers(1, &myBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, myBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

}

void GLWidget::resizeGL(int w, int h)
{
}

void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

To fully answer your question, your "undefined reference" error may be caused by glew32s.lib provided with the glew package which is compiled for MSVC, not MinGW.

Unless you are sure to use the same compiler in your project than the one used to compile the dependencies, the best approach is to compile your own version from the sources.

Upvotes: 2

Related Questions